-
Is it possible to group subcommands similar to https://ajalt.github.io/clikt/documenting/#grouping-options-in-help I'm building a command with a large number of subcommands and it would be great to group/categorize them in the help output. Similar to the output of running
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So one option is to write the grouped commands in the root command help manually, which is what git does. But if you want to automate it, it's not too hard to customize the help formatter to group subcommands by using e.g. a help tag to specify the group: class MyHelpFormatter(context: Context) : MordantHelpFormatter(context) {
override fun shouldShowTag(tag: String, value: String): Boolean {
return super.shouldShowTag(tag, value) && tag != "group"
}
override fun renderCommands(parameters: List<ParameterHelp>): List<RenderedSection> {
val grouped = parameters.filterIsInstance<ParameterHelp.Subcommand>()
.groupBy { it.tags["group"] }
return grouped.mapNotNull { (group, commands) ->
super.renderCommands(commands).firstOrNull()?.let {
if (group == null) it else it.copy(title = Text(styleSectionTitle(group)))
}
}
}
}
fun main(args: Array<String>) {
val root = NoOpCliktCommand(name = "git", help="These are common Git commands used in various situations:").context { helpFormatter = { MyHelpFormatter(it) } }
val g1 = mapOf("group" to "start a working area (see also: git help tutorial)")
val g2 = mapOf("group" to "work on the current change (see also: git help everyday)")
val g3 = mapOf("group" to "examine the history and state (see also: git help revisions)")
root.subcommands(
NoOpCliktCommand(name="clone", help = "Clone a repository into a new directory", helpTags = g1),
NoOpCliktCommand(name="init", help = "Create an empty Git repository or reinitialize an existing one", helpTags = g1),
NoOpCliktCommand(name="add", help = "Add file contents to the index", helpTags = g2),
NoOpCliktCommand(name="mv", help = "Move or rename a file, a directory, or a symlink", helpTags = g2),
NoOpCliktCommand(name="restore", help = "Restore working tree files", helpTags = g2),
NoOpCliktCommand(name="rm", help = "Remove files from the working tree and from the index", helpTags = g2),
NoOpCliktCommand(name="bisect", help = "Use binary search to find the commit that introduced a bug", helpTags = g3),
NoOpCliktCommand(name="diff", help = "Show changes between commits, commit and working tree, etc", helpTags = g3),
NoOpCliktCommand(name="grep", help = "Print lines matching a pattern", helpTags = g3),
NoOpCliktCommand(name="log", help = "Show commit logs", helpTags = g3),
NoOpCliktCommand(name="show", help = "Show various types of objects", helpTags = g3),
NoOpCliktCommand(name="status", help = "Show the working tree status", helpTags = g3),
)
root.main(args)
} |
Beta Was this translation helpful? Give feedback.
So one option is to write the grouped commands in the root command help manually, which is what git does. But if you want to automate it, it's not too hard to customize the help formatter to group subcommands by using e.g. a help tag to specify the group: