rename Arguments to Options, implement Arguments
This commit is contained in:
@@ -1,35 +1,22 @@
|
||||
package com.infendro.cli.command
|
||||
|
||||
import com.infendro.cli.argument.Argument
|
||||
import com.infendro.cli.exception.CliException
|
||||
import com.infendro.cli.exception.build.*
|
||||
import com.infendro.cli.exception.run.UnknownCommandException
|
||||
import com.infendro.cli.util.Regex.ARGUMENT_NAME
|
||||
import com.infendro.cli.util.Regex.COMMAND_NAME
|
||||
import com.infendro.cli.exception.run.CliException
|
||||
import com.infendro.cli.util.Regex.COMMAND
|
||||
import com.infendro.cli.util.Regex.OPTION
|
||||
|
||||
class Command private constructor(
|
||||
val name: String?,
|
||||
val commands: List<Command>,
|
||||
val options: List<Option<*>>,
|
||||
val arguments: List<Argument<*>>,
|
||||
private val block: Execution.() -> Unit,
|
||||
internal val execute: Context.() -> Unit,
|
||||
) {
|
||||
val fallback: Boolean
|
||||
get() = name == null
|
||||
|
||||
fun run(args: Array<String>) = try {
|
||||
val index = args.withIndex()
|
||||
.firstOrNull { (_, value) -> value.startsWith("-") }
|
||||
?.index ?: args.size
|
||||
|
||||
val cmd = args.slice(0..<index)
|
||||
val arg = args.slice(index..<args.size)
|
||||
|
||||
var command = this
|
||||
for (c in cmd) {
|
||||
command = command.commands.firstOrNull { it.name == c || it.name == null }
|
||||
?: throw UnknownCommandException(c)
|
||||
}
|
||||
|
||||
val execution = Execution.from(command, cmd, arg)
|
||||
val execute = command.block
|
||||
execution.execute()
|
||||
Parser.parse(this, args).execute()
|
||||
} catch (e: CliException) {
|
||||
println(e.message)
|
||||
}
|
||||
@@ -40,53 +27,78 @@ class Command private constructor(
|
||||
) {
|
||||
private val commands = mutableListOf<Command>()
|
||||
private val arguments = mutableListOf<Argument<*>>()
|
||||
private lateinit var block: Execution.() -> Unit
|
||||
private val options = mutableListOf<Option<*>>()
|
||||
private lateinit var _execute: Context.() -> Unit
|
||||
|
||||
private val nextIndex: Int
|
||||
get() = index + 1
|
||||
|
||||
fun command(name: String? = null, block: Builder.(Key) -> Unit) {
|
||||
if (name != null && !name.matches(COMMAND_NAME))
|
||||
throw IllegalCommandNameException(name)
|
||||
// validate
|
||||
val last = commands.lastOrNull()
|
||||
when {
|
||||
last?.fallback == true -> throw CommandOrderException()
|
||||
name?.matches(COMMAND) == false -> throw IllegalCommandNameException(name)
|
||||
commands.any { it.name == name } -> throw DuplicateCommandException(name)
|
||||
}
|
||||
|
||||
if (commands.any { it.name == name })
|
||||
throw DuplicateCommandException(name)
|
||||
|
||||
val builder = Builder(nextIndex, name)
|
||||
val key = Key(nextIndex)
|
||||
builder.block(key)
|
||||
val command = builder.build()
|
||||
|
||||
commands.add(command)
|
||||
commands += Builder(nextIndex, name).also {
|
||||
val key = Key(nextIndex)
|
||||
it.block(key)
|
||||
}.build()
|
||||
}
|
||||
|
||||
fun argument(argument: Argument<*>) {
|
||||
for (name in argument.names) {
|
||||
if (!name.matches(ARGUMENT_NAME))
|
||||
throw IllegalArgumentNameException(name)
|
||||
|
||||
if (arguments.any { name in it.names })
|
||||
throw DuplicateArgumentException(name)
|
||||
// validate
|
||||
val last = arguments.lastOrNull()
|
||||
when {
|
||||
last is Argument.Variable -> throw ArgumentOrderException()
|
||||
argument.required && last?.required == false -> throw ArgumentOrderException()
|
||||
}
|
||||
|
||||
arguments.add(argument)
|
||||
arguments += argument
|
||||
}
|
||||
|
||||
fun arguments(vararg arguments: Argument<*>) =
|
||||
arguments.forEach(::argument)
|
||||
fun <T : Argument<*>> T.register() = also(::argument)
|
||||
|
||||
fun execute(block: Execution.() -> Unit) {
|
||||
if (this::block.isInitialized)
|
||||
fun arguments(vararg arguments: Argument<*>) = arguments.forEach(::argument)
|
||||
fun <T : Argument<*>> Array<T>.register() = also(::arguments)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
options += option
|
||||
}
|
||||
|
||||
fun <T : Option<*>> T.register() = also(::option)
|
||||
|
||||
fun options(vararg options: Option<*>) = options.forEach(::option)
|
||||
fun <T : Option<*>> Array<T>.register() = also(::options)
|
||||
|
||||
fun execute(block: Context.() -> Unit) {
|
||||
if (::_execute.isInitialized)
|
||||
throw DuplicateExecuteException()
|
||||
|
||||
this.block = block
|
||||
_execute = block
|
||||
}
|
||||
|
||||
fun build(): Command {
|
||||
if (!this::block.isInitialized)
|
||||
throw NoExecuteException()
|
||||
// validate
|
||||
val last = commands.lastOrNull()
|
||||
when {
|
||||
last?.fallback == true && arguments.isNotEmpty() -> throw InvalidFallbackException()
|
||||
}
|
||||
|
||||
return Command(name, commands, arguments, block)
|
||||
if (!::_execute.isInitialized)
|
||||
_execute = {}
|
||||
|
||||
return Command(name, commands, options, arguments, _execute)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user