Skip to content

Commit f684782

Browse files
committed
Make option's erroName nullable, add argName param
1 parent c65cbca commit f684782

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222

2323
### Changed
2424

25-
- The `adding` and `positionalList` methods of `ArgParser` have had their
26-
parameters slightly reordered to be consistent with the other methods. This
27-
is an incompatible change. The name(s) come first, if any, followed by
25+
- The `storing`, `adding` and `positionalList` methods of `ArgParser` have had
26+
their parameters slightly reordered to be consistent with the other methods.
27+
This is an incompatible change. The name(s) come first, if any, followed by
2828
`help`. Other parameters appear after `help`, with the `transform` function,
29-
if any, last.
29+
if any, last. It is recommended that clients either pass the transform as a
30+
block (ie: with braces) or as a named parameter, as any future new parameters
31+
will necessarily change its position in the list.
3032

3133
- Delegate and DelegateProvider are now abstract classes with internal
3234
constructors. This makes it much easier for me to separate internal and

src/main/kotlin/ArgParser.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class ArgParser(args: Array<out String>,
5656
fun flagging(vararg names: String, help: String): Delegate<Boolean> =
5757
option<Boolean>(
5858
*names,
59-
errorName = optionNamesToErrorName(names),
6059
help = help) { true }.default(false)
6160

6261
/**
@@ -71,7 +70,6 @@ class ArgParser(args: Array<out String>,
7170
fun counting(vararg names: String, help: String): Delegate<Int> =
7271
option<Int>(
7372
*names,
74-
errorName = optionNamesToErrorName(names),
7573
isRepeating = true,
7674
help = help) { value.orElse { 0 } + 1 }.default(0)
7775

@@ -86,13 +84,14 @@ class ArgParser(args: Array<out String>,
8684
fun <T> storing(
8785
vararg names: String,
8886
help: String,
87+
argName: String? = null,
8988
transform: String.() -> T
9089
): Delegate<T> {
91-
val errorName = optionNamesToErrorName(names)
90+
val nonNullArgName = argName ?: optionNameToArgName(selectRepresentativeOptionName(names))
9291
return option(
9392
*names,
94-
errorName = errorName,
95-
argNames = listOf(errorName),
93+
errorName = nonNullArgName,
94+
argNames = listOf(nonNullArgName),
9695
help = help) { transform(arguments.first()) }
9796
}
9897

@@ -101,8 +100,9 @@ class ArgParser(args: Array<out String>,
101100
*/
102101
fun <T> storing(
103102
help: String,
103+
argName: String? = null,
104104
transform: String.() -> T
105-
) = DelegateProvider { identifier -> storing(identifierToOptionName(identifier), help = help, transform = transform) }
105+
) = DelegateProvider { identifier -> storing(identifierToOptionName(identifier), help = help, argName = argName, transform = transform) }
106106

107107
/**
108108
* Creates a Delegate for a single-argument option that stores and returns the option's argument.
@@ -122,15 +122,15 @@ class ArgParser(args: Array<out String>,
122122
fun <E, T : MutableCollection<E>> adding(
123123
vararg names: String,
124124
help: String,
125+
argName: String? = null,
125126
initialValue: T,
126127
transform: String.() -> E
127128
): Delegate<T> {
128-
val errorName = optionNamesToErrorName(names)
129+
val nonNullArgName = argName ?: optionNameToArgName(selectRepresentativeOptionName(names))
129130
return option<T>(
130131
*names,
131-
errorName = errorName,
132132
help = help,
133-
argNames = listOf(errorName),
133+
argNames = listOf(nonNullArgName),
134134
isRepeating = true) {
135135
val result = value.orElse { initialValue }
136136
result.add(transform(arguments.first()))
@@ -144,10 +144,11 @@ class ArgParser(args: Array<out String>,
144144
*/
145145
fun <E, T : MutableCollection<E>> adding(
146146
help: String,
147+
argName: String? = null,
147148
initialValue: T,
148149
transform: String.() -> E
149150
) = DelegateProvider { identifier ->
150-
adding(identifierToOptionName(identifier), initialValue = initialValue, help = help, transform = transform) }
151+
adding(identifierToOptionName(identifier), help = help, argName = argName, initialValue = initialValue, transform = transform) }
151152

152153
/**
153154
* Creates a Delegate for a single-argument option that adds the option's (transformed) argument to a
@@ -156,8 +157,9 @@ class ArgParser(args: Array<out String>,
156157
fun <T> adding(
157158
vararg names: String,
158159
help: String,
160+
argName: String? = null,
159161
transform: String.() -> T
160-
) = adding(*names, initialValue = mutableListOf(), help = help, transform = transform)
162+
) = adding(*names, help = help, argName = argName, initialValue = mutableListOf(), transform = transform)
161163

162164
/**
163165
* Creates a DelegateProvider for a single-argument option that adds the option's (transformed) argument to a
@@ -208,7 +210,8 @@ class ArgParser(args: Array<out String>,
208210
/**
209211
* Creates a Delegate for an option with the specified names.
210212
* @param names names of options, with leading "-" or "--"
211-
* @param errorName name to use when talking about this option in error messages
213+
* @param errorName name to use when talking about this option in error messages, or null to base it upon the
214+
* option names
212215
* @param help the help text for this option
213216
* @param argNames names of this option's arguments
214217
* @param isRepeating whether or not it make sense to repeat this option -- usually used for options where
@@ -219,15 +222,14 @@ class ArgParser(args: Array<out String>,
219222
// TODO: add optionalArg: Boolean
220223
vararg names: String,
221224
help: String,
222-
// TODO: make errorName nullable, and choose name from option names if null
223-
errorName: String,
225+
errorName: String? = null,
224226
argNames: List<String> = emptyList(),
225227
isRepeating: Boolean = false,
226228
handler: OptionInvocation<T>.() -> T
227229
): Delegate<T> {
228230
val delegate = OptionDelegate<T>(
229231
parser = this,
230-
errorName = errorName,
232+
errorName = errorName ?: optionNameToArgName(selectRepresentativeOptionName(names)),
231233
help = help,
232234
optionNames = listOf(*names),
233235
argNames = argNames.toList(),
@@ -760,10 +762,6 @@ class ArgParser(args: Array<out String>,
760762
return names[0]
761763
}
762764

763-
internal fun optionNamesToErrorName(names: Array<out String>): String {
764-
return optionNameToArgName(selectRepresentativeOptionName(names))
765-
}
766-
767765
private fun optionNameToArgName(name: String) =
768766
LEADING_HYPHENS.replace(name, "").toUpperCase().replace('-', '_')
769767

src/test/kotlin/ArgParserTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ fun nullableString() : String? = null
10921092

10931093
class NullableOptionalTest : Test({
10941094
class Args(parser: ArgParser) {
1095-
val path by parser.storing("The path", ::File)
1095+
val path by parser.storing("The path", transform = ::File)
10961096
.default(nullableString()?.let(::File))
10971097

10981098
}
@@ -1113,9 +1113,9 @@ class NullableOptional_withoutTransformTest : Test({
11131113

11141114
class DefaultGeneralizationTest : Test({
11151115
class Args(parser: ArgParser) {
1116-
val shape by parser.storing("The path", ::Rectangle)
1116+
val shape by parser.storing("The path", transform = ::Rectangle)
11171117
.default(Circle())
1118-
val rect by parser.storing("The path", ::Rectangle)
1118+
val rect by parser.storing("The path", transform = ::Rectangle)
11191119
}
11201120
val args = Args(parserOf("--rect=foo"))
11211121
staticType(args.shape) shouldBe Shape::class

0 commit comments

Comments
 (0)