Skip to content

Commit ca753b8

Browse files
committed
Change positional auto-names to UPPER-WITH-HYPHENS
1 parent e82c5ca commit ca753b8

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
previously, automatic option naming would turn "camelCase" into
1515
"--camelCase". Now it is converted to "--camel-case".
1616

17+
- Likewise, positinal argument auto-naming used to convert "camelCase" into
18+
"CAMELCASE". Now it is converted to "CAMEL-CASE".
19+
1720
- Improve help formatting w/long program names
1821

1922
### Fixed

src/main/kotlin/ArgParser.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,19 @@ class ArgParser(args: Array<out String>,
431431
fun identifierToOptionName(identifier: String): String {
432432
return when (identifier.length) {
433433
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-
}
434+
else -> "--" + identifier.camelCaseToUnderscored()
438435
}
439436
}
440437

438+
private fun String.camelCaseToUnderscored(): String {
439+
return replace('_', '-')
440+
.replace(Regex("(\\p{javaLowerCase})(\\p{javaUpperCase})")) { m ->
441+
m.groups[1]!!.value + "-" + m.groups[2]!!.value.toLowerCase()
442+
}
443+
}
444+
441445
fun identifierToArgName(identifier: String): String {
442-
return identifier.toUpperCase()
446+
return identifier.camelCaseToUnderscored().toUpperCase()
443447
}
444448
}
445449
}

src/test/kotlin/ArgParserTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ class ImplicitLongFlagNameTest : Test({
11071107
flag1 shouldBe false
11081108
}
11091109
}.run {
1110-
message shouldBe "missing POSITIONALLIST_INT operand"
1110+
message shouldBe "missing POSITIONAL-LIST-INT operand"
11111111
}
11121112
})
11131113

@@ -1251,7 +1251,7 @@ class AutoNamedPositionalTest : Test({
12511251
shouldThrow<MissingRequiredPositionalArgumentException> {
12521252
Args(parserOf()).autoPositional
12531253
}.run {
1254-
message shouldBe "missing AUTOPOSITIONAL operand"
1254+
message shouldBe "missing AUTO-POSITIONAL operand"
12551255
}
12561256
Args(parserOf("foo")).autoPositional shouldBe "foo"
12571257
})
@@ -1264,7 +1264,7 @@ class AutoNamedPositionalWithTransformTest : Test({
12641264
shouldThrow<MissingRequiredPositionalArgumentException> {
12651265
Args(parserOf()).autoPositional
12661266
}.run {
1267-
message shouldBe "missing AUTOPOSITIONAL operand"
1267+
message shouldBe "missing AUTO-POSITIONAL operand"
12681268
}
12691269
Args(parserOf("47")).autoPositional shouldBe 47
12701270
})
@@ -1277,7 +1277,7 @@ class AutoNamedPositionalListTest : Test({
12771277
shouldThrow<MissingRequiredPositionalArgumentException> {
12781278
Args(parserOf()).autoPositional
12791279
}.run {
1280-
message shouldBe "missing AUTOPOSITIONAL operand"
1280+
message shouldBe "missing AUTO-POSITIONAL operand"
12811281
}
12821282
Args(parserOf("foo")).autoPositional shouldBe listOf("foo")
12831283
})
@@ -1290,7 +1290,7 @@ class AutoNamedPositionalListWithTransformTest : Test({
12901290
shouldThrow<MissingRequiredPositionalArgumentException> {
12911291
Args(parserOf()).autoPositional
12921292
}.run {
1293-
message shouldBe "missing AUTOPOSITIONAL operand"
1293+
message shouldBe "missing AUTO-POSITIONAL operand"
12941294
}
12951295
Args(parserOf("47")).autoPositional shouldBe listOf(47)
12961296
Args(parserOf("27", "38")).autoPositional shouldBe listOf(27, 38)

0 commit comments

Comments
 (0)