Skip to content

Commit 95ce4f0

Browse files
authored
Also strip plural "Commands" when inferring command names (#490)
Sometimes a `NoOpCliktCommand` is used to just group further sub-commands. Such a class may then be called e.g. "ListCommands" with plural "Commands". Support stripping that suffix for such cases.
1 parent 5cb5ab1 commit 95ce4f0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,14 @@ private fun Any.classSimpleName(): String = this::class.simpleName.orEmpty().spl
539539

540540
private fun CliktCommand.inferCommandName(): String {
541541
val name = classSimpleName()
542-
if (name == "Command") return "command"
543-
return name.removeSuffix("Command").replace(Regex("([a-z])([A-Z])")) {
542+
543+
val suffixes = setOf("Command", "Commands")
544+
if (name in suffixes) return name.lowercase()
545+
546+
val nameWithoutSuffixes = suffixes.fold(name) { acc, s -> acc.removeSuffix(s) }
547+
val lowerUpperRegex = Regex("([a-z])([A-Z])")
548+
549+
return nameWithoutSuffixes.replace(lowerUpperRegex) {
544550
"${it.groupValues[1]}-${it.groupValues[2]}"
545551
}.lowercase()
546552
}

clikt/src/commonTest/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.github.ajalt.clikt.parameters.groups.OptionGroup
66
import com.github.ajalt.clikt.parameters.groups.cooccurring
77
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
88
import com.github.ajalt.clikt.parameters.groups.provideDelegate
9-
import com.github.ajalt.clikt.parameters.options.defaultLazy
109
import com.github.ajalt.clikt.parameters.options.flag
1110
import com.github.ajalt.clikt.parameters.options.option
1211
import com.github.ajalt.clikt.parameters.options.required
@@ -35,9 +34,11 @@ class CliktCommandTest {
3534
class ListAllValuesCommand : TestCommand()
3635
class LGTMMeansLookingGoodToMe : TestCommand()
3736
class `nothing-to-change` : TestCommand()
37+
class ListCommands : NoOpCliktCommand()
3838
ListAllValuesCommand().commandName shouldBe "list-all-values"
3939
LGTMMeansLookingGoodToMe().commandName shouldBe "lgtmmeans-looking-good-to-me"
4040
`nothing-to-change`().commandName shouldBe "nothing-to-change"
41+
ListCommands().commandName shouldBe "list"
4142
}
4243

4344
@Test

0 commit comments

Comments
 (0)