Skip to content

Commit

Permalink
Improve the logic to infer command names (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschuberth authored Apr 30, 2020
1 parent 76e4942 commit 97a0292
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class CliktCommand(
private val autoCompleteEnvvar: String? = "",
internal val allowMultipleSubcommands: Boolean = false
) : ParameterHolder {
val commandName = name ?: classSimpleName().toLowerCase()
val commandName = name ?: inferCommandName(classSimpleName())
val commandHelp = help
val commandHelpEpilog = epilog
internal var _subcommands: List<CliktCommand> = emptyList()
Expand Down Expand Up @@ -371,3 +371,8 @@ fun <T : CliktCommand> T.context(block: Context.Builder.() -> Unit): T = apply {
}

private fun Any.classSimpleName(): String = this::class.simpleName.orEmpty().split("$").last()

internal fun inferCommandName(name: String) =
name.removeSuffix("Command").replace(Regex("([a-z])([A-Z])")) {
"${it.groupValues[1]}-${it.groupValues[2]}"
}.toLowerCase()
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import kotlin.test.Test

@Suppress("unused")
class CliktCommandTest {
@Test
@JsName("commandNameInferred")
fun `command name inferred`() {
inferCommandName("ListAllValuesCommand") shouldBe "list-all-values"
inferCommandName("LGTMMeansLookingGoodToMe") shouldBe "lgtmmeans-looking-good-to-me"
inferCommandName("nothing-to-change") shouldBe "nothing-to-change"
}

@Test
@JsName("invokeWithoutSubcommand_false")
fun `invokeWithoutSubcommand=false`() {
Expand Down

0 comments on commit 97a0292

Please sign in to comment.