improve argument creation, improve fallback command

This commit is contained in:
2026-01-05 16:44:07 +01:00
parent 6779e28792
commit 86409e9f72
4 changed files with 26 additions and 152 deletions

View File

@@ -35,16 +35,26 @@ class Command private constructor(
private val key: Key
get() = Key(level)
fun command(name: String? = null, block: Builder.(Key) -> Unit) {
fun command(name: String, block: Builder.() -> Unit) {
// validate
val last = commands.lastOrNull()
when {
last != null && last.fallback -> throw CommandOrderException()
name != null && !name.matches(COMMAND) -> throw IllegalCommandNameException(name)
!name.matches(COMMAND) -> throw IllegalCommandNameException(name)
commands.any { it.name == name } -> throw DuplicateCommandException(name)
}
commands += Builder(level + 1, name).also { it.block(it.key) }.build()
commands += Builder(level + 1, name).also(block).build()
}
fun fallback(block: Builder.(Key) -> Unit) {
// validate
val last = commands.lastOrNull()
when {
last != null && last.fallback -> throw CommandOrderException()
}
commands += Builder(level + 1, null).also { it.block(it.key) }.build()
}
fun argument(argument: Argument<*>) {