Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also strip plural "Commands" when inferring command names #490

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,14 @@ private fun Any.classSimpleName(): String = this::class.simpleName.orEmpty().spl

private fun CliktCommand.inferCommandName(): String {
val name = classSimpleName()
if (name == "Command") return "command"
return name.removeSuffix("Command").replace(Regex("([a-z])([A-Z])")) {

val suffixes = setOf("Command", "Commands")
if (name in suffixes) return name.lowercase()

val nameWithoutSuffixes = suffixes.fold(name) { acc, s -> acc.removeSuffix(s) }
val lowerUpperRegex = Regex("([a-z])([A-Z])")

return nameWithoutSuffixes.replace(lowerUpperRegex) {
"${it.groupValues[1]}-${it.groupValues[2]}"
}.lowercase()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.groups.cooccurring
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
import com.github.ajalt.clikt.parameters.groups.provideDelegate
import com.github.ajalt.clikt.parameters.options.defaultLazy
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
Expand Down Expand Up @@ -35,9 +34,11 @@ class CliktCommandTest {
class ListAllValuesCommand : TestCommand()
class LGTMMeansLookingGoodToMe : TestCommand()
class `nothing-to-change` : TestCommand()
class ListCommands : NoOpCliktCommand()
ListAllValuesCommand().commandName shouldBe "list-all-values"
LGTMMeansLookingGoodToMe().commandName shouldBe "lgtmmeans-looking-good-to-me"
`nothing-to-change`().commandName shouldBe "nothing-to-change"
ListCommands().commandName shouldBe "list"
}

@Test
Expand Down
Loading