implement descriptions, improve default help

This commit is contained in:
2026-01-09 20:13:19 +01:00
parent 47834c91d8
commit ff87ae90f5
9 changed files with 204 additions and 108 deletions

View File

@@ -13,6 +13,7 @@ import com.infendro.cli.util.Regex
sealed class Command private constructor(
val name: String,
val description: String?,
val commands: List<Command>,
val arguments: List<Argument<*>>,
val options: List<Option<*>>,
@@ -28,32 +29,33 @@ sealed class Command private constructor(
is ContextParser.Result.Success -> result.context.execute()
is ContextParser.Result.Help -> {
val command = result.path.last()
command.renderer.render(result.path)?.let { print(it) }
val help = command.renderer.render(result.path)
if (help != null) print(help)
}
is ContextParser.Result.Failure -> {
println("error: ${result.failure.message}")
val command = result.path.last()
command.renderer.render(result.path)?.let {
println()
print(it)
}
val help = command.renderer.render(result.path)
if (help != null) print("\n$help")
}
}
}
class Named(
name: String,
description: String?,
commands: List<Command>,
arguments: List<Argument<*>>,
options: List<Option<*>>,
execute: Context.() -> Unit,
renderer: HelpRenderer,
) : Command(name, commands, arguments, options, execute, renderer) {
) : Command(name, description, commands, arguments, options, execute, renderer) {
class Builder(
name: String,
) : Command.Builder<Named>(name) {
description: String?,
) : Command.Builder<Named>(name, description) {
override fun create() =
Named(name, commands, arguments, options, execute, renderer)
Named(name, description, commands, arguments, options, execute, renderer)
}
}
@@ -61,20 +63,22 @@ sealed class Command private constructor(
val key: Key<T>,
val parser: Parser<T>,
name: String,
description: String?,
commands: List<Command>,
arguments: List<Argument<*>>,
options: List<Option<*>>,
execute: Context.() -> Unit,
renderer: HelpRenderer,
) : Command(name, commands, arguments, options, execute, renderer) {
) : Command(name, description, commands, arguments, options, execute, renderer) {
class Builder<T : Any>(
val parser: Parser<T>,
name: String,
) : Command.Builder<Fallback<T>>(name) {
description: String?,
) : Command.Builder<Fallback<T>>(name, description) {
internal val key = Key<T>()
override fun create() =
Fallback(key, parser, name, commands, arguments, options, execute, renderer)
Fallback(key, parser, name, description, commands, arguments, options, execute, renderer)
}
class Key<T>
@@ -83,6 +87,7 @@ sealed class Command private constructor(
@Dsl
sealed class Builder<COMMAND : Command>(
protected val name: String,
protected val description: String?,
) {
protected val commands = mutableListOf<Command>()
protected val arguments = mutableListOf<Argument<*>>()