Skip to content

Commit e82c5ca

Browse files
committed
Fix github issue #14
Change `identifierToOptionName` so that it converts "fooBarBaz" into "--foo-bar-baz".
1 parent 194e9b3 commit e82c5ca

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
1011
### Changed
1112

12-
- Fixed [Issue #15](https://github.com/xenomachina/kotlin-argparser/issues/15)
13+
- [Issue #14](https://github.com/xenomachina/kotlin-argparser/issues/14)
14+
previously, automatic option naming would turn "camelCase" into
15+
"--camelCase". Now it is converted to "--camel-case".
1316

1417
- Improve help formatting w/long program names
1518

19+
### Fixed
20+
21+
- [Issue #15](https://github.com/xenomachina/kotlin-argparser/issues/15)
22+
— make it possible to specify 'argName' on all variants of 'storing' and
23+
`adding`
24+
25+
1626
## 2.0.0 - 2017-04-21
1727

1828
### Added

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,28 @@ which are in turn each represented by properties that delegate to an
2121

2222
```kotlin
2323
class MyArgs(parser: ArgParser) {
24-
val verbose by parser.flagging(help = "enable verbose mode")
24+
val v by parser.flagging(help = "enable verbose mode")
2525

26-
val name by parser.storing(help = "name of the widget")
26+
val widgetName by parser.storing("name of the widget")
2727

28-
val size by parser.storing(help = "size of the plumbus") { toInt() }
28+
val size by parser.storing("size of the plumbus") { toInt() }
2929
}
3030
```
3131

32-
The names of an option is inferred from the name of the property it's bound to.
33-
Direct control over the option name is also possible, and for most types of
34-
options it's also possible to have multiple names (typically used for a short
35-
and long name):
32+
The name of an option is inferred from the name of the property it is bound to.
33+
The options above are named "-v", "--widget-name" and "--size",
34+
respectively.
35+
36+
Direct control over an option's name is also possible, and for most types of
37+
options it is also possible to have multiple names (typically used to have both
38+
a short and long name):
3639

3740
```kotlin
3841
class MyArgs(parser: ArgParser) {
3942
val verbose by parser.flagging("-v", "--verbose",
40-
help="enable verbose mode")
43+
help = "enable verbose mode")
4144

42-
val name by parser.storing("-N", "--name",
45+
val name by parser.storing("-w", "--widget-name",
4346
help = "name of the widget")
4447

4548
val size by parser.storing("-s", "--size",
@@ -153,7 +156,7 @@ For a single positional argument:
153156

154157
```kotlin
155158
val destination by parser.positional("DEST",
156-
help="destination filename")
159+
help = "destination filename")
157160
```
158161

159162
The name ("DEST", here) is used in error handling and help text.
@@ -162,7 +165,7 @@ For a list of positional arguments:
162165

163166
```kotlin
164167
val sources by parser.positionalList("SOURCE", 1..Int.MAX_VALUE,
165-
help="source filename")
168+
help = "source filename")
166169
```
167170

168171
The range indicates how many arguments should be collected, and actually
@@ -174,10 +177,10 @@ arguments from `String` to whatever type is actually desired:
174177

175178
```kotlin
176179
val destination by parser.positional("DEST",
177-
help="...") { File(this) }
180+
help = "...") { File(this) }
178181

179182
val sources by parser.positionalList("SOURCE", 1..Int.MAX_VALUE,
180-
help="...") { File(this) }
183+
help = "...") { File(this) }
181184
```
182185

183186

@@ -191,13 +194,13 @@ optional attributes:
191194
value is provided. This is done with the `default` method:
192195

193196
```kotlin
194-
val name by parser.storing("-N", "--name", help="...").default("John Doe")
197+
val name by parser.storing("-N", "--name", help = "...").default("John Doe")
195198
```
196199

197200
Note that it *is* possible to use `null` for the default:
198201

199202
```kotlin
200-
val name by parser.storing("-N", "--name", help="...").default(null)
203+
val name by parser.storing("-N", "--name", help = "...").default(null)
201204
```
202205

203206
The resulting value will be nullable (a `String?` in this case).
@@ -206,7 +209,7 @@ optional attributes:
206209
case the `addValidator` method can be used.
207210

208211
```kotlin
209-
val percentages by parser.adding("--percentages", help="...") { toInt() }
212+
val percentages by parser.adding("--percentages", help = "...") { toInt() }
210213
.addValidator {
211214
if (sum() != 100)
212215
throw InvalidArgumentException(

src/main/kotlin/ArgParser.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,13 @@ class ArgParser(args: Array<out String>,
429429

430430
companion object {
431431
fun identifierToOptionName(identifier: String): String {
432-
return if (identifier.length == 1) ("-" + identifier) else ("--" + identifier.replace('_', '-'))
432+
return when (identifier.length) {
433+
1 -> "-" + identifier
434+
else -> "--" + identifier.replace('_', '-')
435+
.replace(Regex("(\\p{javaLowerCase})(\\p{javaUpperCase})")) { m ->
436+
m.groups[1]!!.value + "-" + m.groups[2]!!.value.toLowerCase()
437+
}
438+
}
433439
}
434440

435441
fun identifierToArgName(identifier: String): String {

src/test/kotlin/ArgParserTest.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,16 +1171,16 @@ class AutoNamedFlaggingTest : Test({
11711171
val autoFlag by parser.flagging(TEST_HELP)
11721172
}
11731173
Args(parserOf()).autoFlag shouldBe false
1174-
Args(parserOf("--autoFlag")).autoFlag shouldBe true
1174+
Args(parserOf("--auto-flag")).autoFlag shouldBe true
11751175
})
11761176

11771177
class AutoNamedCountingTest : Test({
11781178
class Args(parser: ArgParser) {
11791179
val autoCount by parser.counting(TEST_HELP)
11801180
}
11811181
Args(parserOf()).autoCount shouldBe 0
1182-
Args(parserOf("--autoCount")).autoCount shouldBe 1
1183-
Args(parserOf("--autoCount", "--autoCount")).autoCount shouldBe 2
1182+
Args(parserOf("--auto-count")).autoCount shouldBe 1
1183+
Args(parserOf("--auto-count", "--auto-count")).autoCount shouldBe 2
11841184
})
11851185

11861186
class AutoNamedStoringTest : Test({
@@ -1191,11 +1191,11 @@ class AutoNamedStoringTest : Test({
11911191
shouldThrow<MissingValueException> {
11921192
Args(parserOf()).autoStore
11931193
}.run {
1194-
message shouldBe "missing AUTOSTORE"
1194+
message shouldBe "missing AUTO_STORE"
11951195
}
11961196

1197-
Args(parserOf("--autoStore=foo")).autoStore shouldBe "foo"
1198-
Args(parserOf("--autoStore", "bar", "--autoStore", "baz")).autoStore shouldBe "baz"
1197+
Args(parserOf("--auto-store=foo")).autoStore shouldBe "foo"
1198+
Args(parserOf("--auto-store", "bar", "--auto-store", "baz")).autoStore shouldBe "baz"
11991199
})
12001200

12011201
class AutoNamedStoringWithTransformTest : Test({
@@ -1206,11 +1206,11 @@ class AutoNamedStoringWithTransformTest : Test({
12061206
shouldThrow<MissingValueException> {
12071207
Args(parserOf()).autoStore
12081208
}.run {
1209-
message shouldBe "missing AUTOSTORE"
1209+
message shouldBe "missing AUTO_STORE"
12101210
}
12111211

1212-
Args(parserOf("--autoStore=5")).autoStore shouldBe 5
1213-
Args(parserOf("--autoStore", "11", "--autoStore", "42")).autoStore shouldBe 42
1212+
Args(parserOf("--auto-store=5")).autoStore shouldBe 5
1213+
Args(parserOf("--auto-store", "11", "--auto-store", "42")).autoStore shouldBe 42
12141214
})
12151215

12161216
class AutoNamedAddingTest : Test({
@@ -1219,8 +1219,8 @@ class AutoNamedAddingTest : Test({
12191219
}
12201220

12211221
Args(parserOf()).autoAccumulator shouldBe emptyList<String>()
1222-
Args(parserOf("--autoAccumulator=foo")).autoAccumulator shouldBe listOf("foo")
1223-
Args(parserOf("--autoAccumulator", "bar", "--autoAccumulator", "baz")).autoAccumulator shouldBe listOf("bar", "baz")
1222+
Args(parserOf("--auto-accumulator=foo")).autoAccumulator shouldBe listOf("foo")
1223+
Args(parserOf("--auto-accumulator", "bar", "--auto-accumulator", "baz")).autoAccumulator shouldBe listOf("bar", "baz")
12241224
})
12251225

12261226
class AutoNamedAddingWithTransformTest : Test({
@@ -1229,8 +1229,8 @@ class AutoNamedAddingWithTransformTest : Test({
12291229
}
12301230

12311231
Args(parserOf()).autoAccumulator shouldBe emptyList<Int>()
1232-
Args(parserOf("--autoAccumulator=5")).autoAccumulator shouldBe listOf(5)
1233-
Args(parserOf("--autoAccumulator", "11", "--autoAccumulator", "42")).autoAccumulator shouldBe listOf(11, 42)
1232+
Args(parserOf("--auto-accumulator=5")).autoAccumulator shouldBe listOf(5)
1233+
Args(parserOf("--auto-accumulator", "11", "--auto-accumulator", "42")).autoAccumulator shouldBe listOf(11, 42)
12341234
})
12351235

12361236
class AutoNamedAddingWithTransformAndInitialTest : Test({
@@ -1239,8 +1239,8 @@ class AutoNamedAddingWithTransformAndInitialTest : Test({
12391239
}
12401240

12411241
Args(parserOf()).autoAccumulator shouldBe emptySet<Int>()
1242-
Args(parserOf("--autoAccumulator=5")).autoAccumulator shouldBe setOf(5)
1243-
Args(parserOf("--autoAccumulator", "11", "--autoAccumulator", "42")).autoAccumulator shouldBe setOf(42, 11)
1242+
Args(parserOf("--auto-accumulator=5")).autoAccumulator shouldBe setOf(5)
1243+
Args(parserOf("--auto-accumulator", "11", "--auto-accumulator", "42")).autoAccumulator shouldBe setOf(42, 11)
12441244
})
12451245

12461246
class AutoNamedPositionalTest : Test({

0 commit comments

Comments
 (0)