implement optional variable argument/option ranges, refactor exception handling
adapt required/optional argument handling implement bounded/unbounded variable argument handling
This commit is contained in:
@@ -4,31 +4,31 @@ import com.infendro.cli.Dsl
|
||||
import com.infendro.cli.command.context.Context
|
||||
import com.infendro.cli.command.context.Parser
|
||||
import com.infendro.cli.exception.build.*
|
||||
import com.infendro.cli.exception.run.CliException
|
||||
import com.infendro.cli.exception.run.RunException
|
||||
import com.infendro.cli.util.Regex.ARGUMENT
|
||||
import com.infendro.cli.util.Regex.COMMAND
|
||||
import com.infendro.cli.util.Regex.OPTION
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Command private constructor(
|
||||
val name: String?,
|
||||
val name: String,
|
||||
val fallback: Boolean,
|
||||
val commands: List<Command>,
|
||||
val arguments: List<Argument<*>>,
|
||||
val options: List<Option<*>>,
|
||||
internal val execute: Context.() -> Unit,
|
||||
) {
|
||||
val fallback: Boolean
|
||||
get() = name == null
|
||||
|
||||
fun run(args: Array<String>) = try {
|
||||
Parser.parse(this, args).execute()
|
||||
} catch (e: CliException) {
|
||||
} catch (e: RunException) {
|
||||
println(e.message)
|
||||
}
|
||||
|
||||
@Dsl
|
||||
class Builder internal constructor(
|
||||
private val level: Int,
|
||||
private val name: String?,
|
||||
private val name: String,
|
||||
private val fallback: Boolean,
|
||||
) {
|
||||
private val commands = mutableListOf<Command>()
|
||||
private val arguments = mutableListOf<Argument<*>>()
|
||||
@@ -39,35 +39,17 @@ class Command private constructor(
|
||||
get() = Key(level)
|
||||
|
||||
fun command(name: String, block: Builder.() -> Unit) {
|
||||
// validate
|
||||
val last = commands.lastOrNull()
|
||||
when {
|
||||
last != null && last.fallback -> throw CommandOrderException()
|
||||
!name.matches(COMMAND) -> throw IllegalCommandNameException(name)
|
||||
commands.any { it.name == name } -> throw DuplicateCommandException(name)
|
||||
}
|
||||
|
||||
commands += Builder(level + 1, name).also(block).build()
|
||||
validateCommand(name)
|
||||
commands += Builder(level + 1, name, fallback = false).apply(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 fallback(name: String, block: Builder.(Key) -> Unit) {
|
||||
validateCommand(name)
|
||||
commands += Builder(level + 1, name, fallback = true).apply { block(key) }.build()
|
||||
}
|
||||
|
||||
fun argument(argument: Argument<*>) {
|
||||
// validate
|
||||
val last = arguments.lastOrNull()
|
||||
when {
|
||||
last is Argument.Variable -> throw ArgumentOrderException()
|
||||
argument.required && last != null && !last.required -> throw ArgumentOrderException()
|
||||
}
|
||||
|
||||
validateArgument(argument)
|
||||
arguments += argument
|
||||
}
|
||||
|
||||
@@ -77,14 +59,7 @@ class Command private constructor(
|
||||
operator fun <T : Argument<*>> T.getValue(thisRef: Nothing?, property: KProperty<*>) = this
|
||||
|
||||
fun option(option: Option<*>) {
|
||||
// validate
|
||||
for (name in option.names) {
|
||||
when {
|
||||
!name.matches(OPTION) -> throw IllegalOptionNameException(name)
|
||||
options.any { name in it.names } -> throw DuplicateOptionException(name)
|
||||
}
|
||||
}
|
||||
|
||||
validateOption(option)
|
||||
options += option
|
||||
}
|
||||
|
||||
@@ -94,23 +69,62 @@ class Command private constructor(
|
||||
operator fun <T : Option<*>> T.getValue(thisRef: Nothing?, property: KProperty<*>) = this
|
||||
|
||||
fun execute(block: Context.() -> Unit) {
|
||||
if (::_execute.isInitialized)
|
||||
throw DuplicateExecuteException()
|
||||
|
||||
validateExecute()
|
||||
_execute = block
|
||||
}
|
||||
|
||||
fun build(): Command {
|
||||
// validate
|
||||
val last = commands.lastOrNull()
|
||||
when {
|
||||
last != null && last.fallback && arguments.isNotEmpty() -> throw InvalidFallbackException()
|
||||
}
|
||||
|
||||
if (!::_execute.isInitialized)
|
||||
_execute = {}
|
||||
|
||||
return Command(name, commands, arguments, options, _execute)
|
||||
validate()
|
||||
return Command(name, fallback, commands, arguments, options, _execute)
|
||||
}
|
||||
|
||||
private fun validateCommand(name: String) {
|
||||
if (commands.isNotEmpty()) {
|
||||
val last = commands.last()
|
||||
when {
|
||||
last.fallback -> throw InvalidCommandOrder()
|
||||
}
|
||||
}
|
||||
when {
|
||||
!name.matches(COMMAND) -> throw InvalidCommand(name)
|
||||
commands.any { it.name == name } -> throw DuplicateCommand(name)
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateArgument(argument: Argument<*>) {
|
||||
if (arguments.isNotEmpty()) {
|
||||
val last = arguments.last()
|
||||
when {
|
||||
last.unbounded -> throw InvalidArgumentOrder()
|
||||
argument.required && last.optional -> throw InvalidArgumentOrder()
|
||||
}
|
||||
}
|
||||
when {
|
||||
!argument.name.matches(ARGUMENT) -> throw InvalidArgument(argument.name)
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateOption(option: Option<*>) {
|
||||
for (name in option.names) {
|
||||
when {
|
||||
!name.matches(OPTION) -> throw InvalidOption(name)
|
||||
options.any { name in it.names } -> throw DuplicateOption(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateExecute() {
|
||||
if (::_execute.isInitialized)
|
||||
throw DuplicateExecute()
|
||||
}
|
||||
|
||||
private fun validate() {
|
||||
if (commands.any { it.fallback } && arguments.isNotEmpty()) {
|
||||
throw InvalidFallback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,5 +133,5 @@ class Command private constructor(
|
||||
)
|
||||
}
|
||||
|
||||
fun cli(block: Command.Builder.() -> Unit) =
|
||||
Command.Builder(-1, null).also(block).build()
|
||||
fun cli(name: String, block: Command.Builder.() -> Unit) =
|
||||
Command.Builder(-1, name, fallback = false).apply(block).build()
|
||||
|
||||
Reference in New Issue
Block a user