Skip to content

Commit ad53115

Browse files
committed
Fix github issue #15
Some of the storing and adding overloads were missing the argNames parameter.
1 parent 341060e commit ad53115

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/main/kotlin/ArgParser.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ class ArgParser(args: Array<out String>,
107107
/**
108108
* Creates a Delegate for a single-argument option that stores and returns the option's argument.
109109
*/
110-
fun storing(vararg names: String, help: String): Delegate<String> =
111-
storing(*names, help = help) { this }
110+
fun storing(vararg names: String, help: String, argName: String? = null): Delegate<String> =
111+
storing(*names, help = help, argName = argName) { this }
112112

113113
/**
114114
* Creates a DelegateProvider for a single-argument option that stores and returns the option's argument.
115115
*/
116-
fun storing(help: String) = DelegateProvider { identifier -> storing(identifierToOptionName(identifier), help = help) }
116+
fun storing(help: String, argName: String? = null) =
117+
DelegateProvider { identifier ->
118+
storing(identifierToOptionName(identifier), help = help, argName = argName) }
117119

118120
/**
119121
* Creates a Delegate for a single-argument option that adds the option's (transformed) argument to a
@@ -167,16 +169,17 @@ class ArgParser(args: Array<out String>,
167169
*/
168170
fun <T> adding(
169171
help: String,
172+
argName: String? = null,
170173
transform: String.() -> T
171174
) = DelegateProvider { identifier ->
172-
adding(identifierToOptionName(identifier), help = help, transform = transform) }
175+
adding(identifierToOptionName(identifier), help = help, argName = argName, transform = transform) }
173176

174177
/**
175178
* Creates a Delegate for a single-argument option that adds the option's argument to a MutableList each time the
176179
* option appears in args, and returns said MutableCollection.
177180
*/
178-
fun adding(vararg names: String, help: String): Delegate<MutableList<String>> =
179-
adding(*names, help = help) { this }
181+
fun adding(vararg names: String, help: String, argName: String? = null): Delegate<MutableList<String>> =
182+
adding(*names, help = help, argName = argName) { this }
180183

181184
/**
182185
* Creates a DelegateProvider for a single-argument option that adds the option's argument to a MutableList each time the

src/test/kotlin/ArgParserTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,3 +1418,33 @@ class PositionalListAddValidatorTest : Test({
14181418
message shouldBe "X elements must be even, 37 is odd"
14191419
}
14201420
})
1421+
1422+
class Issue15Test : Test({
1423+
class Args(parser: ArgParser) {
1424+
val manual by parser.storing("--named-by-hand", help = TEST_HELP, argName = "HANDYS-ARG")
1425+
val auto by parser.storing(TEST_HELP, argName = "OTTOS-ARG")
1426+
val foo by parser.adding(help = TEST_HELP, argName = "BAR") { toInt() }
1427+
val bar by parser.adding("--baz", help = TEST_HELP, argName = "QUUX")
1428+
}
1429+
1430+
shouldThrow<ShowHelpException> {
1431+
Args(parserOf("--help")).manual
1432+
}.run {
1433+
// TODO: find a way to make this less brittle (ie: don't use help text)
1434+
StringWriter().apply { printUserMessage(this, null, 10000) }.toString().trim() shouldBe """
1435+
usage: [-h] --named-by-hand HANDYS-ARG --auto OTTOS-ARG [--foo BAR]... [--baz QUUX]...
1436+
1437+
required arguments:
1438+
--named-by-hand HANDYS-ARG test help message
1439+
1440+
--auto OTTOS-ARG test help message
1441+
1442+
1443+
optional arguments:
1444+
-h, --help show this help message and exit
1445+
1446+
--foo BAR test help message
1447+
1448+
--baz QUUX test help message""".trim()
1449+
}
1450+
})

0 commit comments

Comments
 (0)