refactor argument system
- arguments can have multiple names simultaneously - argument values can be passed using spaces (e.g., --arg value) - single-character arguments can be passed using a single dash (e.g., --a -> -a) - multiple single-character arguments can be combined (e.g., -a -b -> -ab) - simplify custom arguments using generic classes
This commit is contained in:
@@ -1,99 +1,37 @@
|
||||
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.ArgumentCountException
|
||||
import com.infendro.cli.exception.run.InvalidArgumentException
|
||||
import com.infendro.cli.exception.run.UnknownArgumentException
|
||||
import com.infendro.cli.exception.run.UnknownCommandException
|
||||
import com.infendro.cli.util.Regex.ARGUMENT
|
||||
import com.infendro.cli.util.Regex.ARGUMENT_NAME
|
||||
import com.infendro.cli.util.Regex.COMMAND_NAME
|
||||
|
||||
class Command private constructor(
|
||||
private val index: Int,
|
||||
private val name: String?,
|
||||
private val commands: List<Command>,
|
||||
private val arguments: List<Argument<*>>,
|
||||
val name: String?,
|
||||
val commands: List<Command>,
|
||||
val arguments: List<Argument<*>>,
|
||||
private val block: Execution.() -> Unit,
|
||||
) {
|
||||
private val nextIndex: Int
|
||||
get() = index + 1
|
||||
|
||||
fun run(
|
||||
args: Array<String>,
|
||||
) {
|
||||
fun run(args: Array<String>) = try {
|
||||
val index = args.withIndex()
|
||||
.find { (_, value) -> value.startsWith("--") }
|
||||
.firstOrNull { (_, value) -> value.startsWith("-") }
|
||||
?.index ?: args.size
|
||||
|
||||
val cmd = args.slice(0..<index)
|
||||
val arg = args.slice(index..<args.size)
|
||||
|
||||
try {
|
||||
run(cmd, arg)
|
||||
} catch (e: UnknownCommandException) {
|
||||
println("""unknown command "${e.command}"""")
|
||||
} catch (e: InvalidArgumentException) {
|
||||
println("""invalid argument "${e.argument}"""")
|
||||
} catch (e: UnknownArgumentException) {
|
||||
println("""unknown argument "${e.argument}"""")
|
||||
} catch (e: ArgumentCountException) {
|
||||
when {
|
||||
e.min == e.max -> println("""argument "${e.argument}" must appear ${e.min} times""")
|
||||
e.max == Int.MAX_VALUE -> println("""argument "${e.argument}" must appear at least ${e.min} times""")
|
||||
else -> println("""argument "${e.argument}" must appear between ${e.min} and ${e.max} times""")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun run(
|
||||
cmd: List<String>,
|
||||
arg: List<String>,
|
||||
) {
|
||||
if (index == cmd.lastIndex) {
|
||||
Execution(
|
||||
parseCmd(cmd),
|
||||
parseArg(arg),
|
||||
).block()
|
||||
} else {
|
||||
val command = commands.firstOrNull() { it.name == cmd[nextIndex] || it.name == null }
|
||||
?: throw UnknownCommandException(cmd[nextIndex])
|
||||
command.run(cmd, arg)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseCmd(
|
||||
cmd: List<String>,
|
||||
): List<Execution.Cmd> {
|
||||
return cmd.map { Execution.Cmd(it) }
|
||||
}
|
||||
|
||||
private fun parseArg(
|
||||
arg: List<String>,
|
||||
): List<Execution.Arg> {
|
||||
val result = mutableListOf<Execution.Arg>()
|
||||
|
||||
for (argument in arg) {
|
||||
val match = ARGUMENT.matchEntire(argument)
|
||||
?: throw InvalidArgumentException(argument)
|
||||
|
||||
val name = match.groups["name"]!!.value
|
||||
val value = match.groups["value"]?.value
|
||||
|
||||
if (arguments.none { it.name == name })
|
||||
throw UnknownArgumentException(name)
|
||||
|
||||
result += Execution.Arg(name, value)
|
||||
var command = this
|
||||
for (c in cmd) {
|
||||
command = command.commands.firstOrNull { it.name == c || it.name == null }
|
||||
?: throw UnknownCommandException(c)
|
||||
}
|
||||
|
||||
arguments.forEach { argument ->
|
||||
val count = result.count { it.name == argument.name }
|
||||
if (count !in argument.min..argument.max)
|
||||
throw ArgumentCountException(argument.name, argument.min, argument.max)
|
||||
}
|
||||
|
||||
return result
|
||||
val execution = Execution.from(command, cmd, arg)
|
||||
val execute = command.block
|
||||
execution.execute()
|
||||
} catch (e: CliException) {
|
||||
println(e.message)
|
||||
}
|
||||
|
||||
class Builder internal constructor(
|
||||
@@ -107,46 +45,37 @@ class Command private constructor(
|
||||
private val nextIndex: Int
|
||||
get() = index + 1
|
||||
|
||||
fun command(
|
||||
name: String? = null,
|
||||
block: Builder.(Key) -> Unit,
|
||||
) {
|
||||
if (name != null && !COMMAND_NAME.matches(name))
|
||||
fun command(name: String? = null, block: Builder.(Key) -> Unit) {
|
||||
if (name != null && !name.matches(COMMAND_NAME))
|
||||
throw IllegalCommandNameException(name)
|
||||
|
||||
if (commands.any { it.name == name })
|
||||
throw DuplicateCommandException(name)
|
||||
|
||||
val builder = Builder(nextIndex, name)
|
||||
builder.block(Key(nextIndex))
|
||||
val key = Key(nextIndex)
|
||||
builder.block(key)
|
||||
val command = builder.build()
|
||||
|
||||
commands.add(command)
|
||||
}
|
||||
|
||||
fun argument(
|
||||
argument: Argument<*>,
|
||||
) {
|
||||
if (!ARGUMENT_NAME.matches(argument.name))
|
||||
throw IllegalArgumentNameException(argument.name)
|
||||
fun argument(argument: Argument<*>) {
|
||||
for (name in argument.names) {
|
||||
if (!name.matches(ARGUMENT_NAME))
|
||||
throw IllegalArgumentNameException(name)
|
||||
|
||||
if (arguments.any { it.name == argument.name })
|
||||
throw DuplicateArgumentException(argument.name)
|
||||
if (arguments.any { name in it.names })
|
||||
throw DuplicateArgumentException(name)
|
||||
}
|
||||
|
||||
arguments.add(argument)
|
||||
}
|
||||
|
||||
fun arguments(
|
||||
vararg arguments: Argument<*>,
|
||||
) {
|
||||
for (argument in arguments) {
|
||||
argument(argument)
|
||||
}
|
||||
}
|
||||
fun arguments(vararg arguments: Argument<*>) =
|
||||
arguments.forEach(::argument)
|
||||
|
||||
fun execute(
|
||||
block: Execution.() -> Unit,
|
||||
) {
|
||||
fun execute(block: Execution.() -> Unit) {
|
||||
if (this::block.isInitialized)
|
||||
throw DuplicateExecuteException()
|
||||
|
||||
@@ -157,7 +86,7 @@ class Command private constructor(
|
||||
if (!this::block.isInitialized)
|
||||
throw NoExecuteException()
|
||||
|
||||
return Command(index, name, commands, arguments, block)
|
||||
return Command(name, commands, arguments, block)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user