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,20 +1,92 @@
|
||||
package com.infendro.cli.command
|
||||
|
||||
import com.infendro.cli.argument.Argument
|
||||
import com.infendro.cli.exception.run.DuplicateArgumentException
|
||||
import com.infendro.cli.exception.run.MalformedArgumentException
|
||||
import com.infendro.cli.exception.run.MissingArgumentException
|
||||
import com.infendro.cli.exception.run.UnknownArgumentException
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Execution(
|
||||
class Execution private constructor(
|
||||
val commands: List<Cmd>,
|
||||
val arguments: List<Arg>,
|
||||
) {
|
||||
companion object {
|
||||
fun from(
|
||||
command: Command,
|
||||
cmd: List<String>,
|
||||
arg: List<String>,
|
||||
): Execution {
|
||||
val commands = Cmd.from(cmd)
|
||||
val arguments = Arg.from(command, arg)
|
||||
return Execution(commands, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
class Cmd(
|
||||
val name: String,
|
||||
)
|
||||
) {
|
||||
companion object {
|
||||
fun from(cmd: List<String>): List<Cmd> = cmd.map { Cmd(it) }
|
||||
}
|
||||
}
|
||||
|
||||
class Arg(
|
||||
val name: String,
|
||||
val value: String?,
|
||||
)
|
||||
) {
|
||||
companion object {
|
||||
fun from(command: Command, arg: List<String>): List<Arg> = buildList {
|
||||
var i = 0
|
||||
while (i < arg.size) {
|
||||
val dashes = arg[i].takeWhile { it == '-' }.count()
|
||||
val trimmed = arg[i].substring(dashes)
|
||||
|
||||
if (dashes !in 1..2 || trimmed.isEmpty())
|
||||
throw MalformedArgumentException(arg[i])
|
||||
|
||||
val (name, value) = when {
|
||||
trimmed.contains('=') -> {
|
||||
val (name, value) = trimmed.split('=', limit = 2)
|
||||
name to value
|
||||
}
|
||||
|
||||
else -> {
|
||||
val next = arg.getOrNull(i + 1)
|
||||
if (next != null && !next.startsWith('-')) {
|
||||
i++
|
||||
trimmed to next
|
||||
} else {
|
||||
trimmed to null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val names = when {
|
||||
dashes == 1 -> name.map { "$it" }
|
||||
else -> listOf(name)
|
||||
}
|
||||
|
||||
for (name in names) {
|
||||
if (command.arguments.none { name in it.names })
|
||||
throw UnknownArgumentException(name)
|
||||
|
||||
//TODO parse early
|
||||
add(Arg(name, value))
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
for (argument in command.arguments) {
|
||||
val count = count { it.name in argument.names }
|
||||
when {
|
||||
argument.required && count == 0 -> throw MissingArgumentException(argument.name)
|
||||
count > 1 -> throw DuplicateArgumentException(argument.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
operator fun Command.Key.getValue(
|
||||
thisRef: Any?,
|
||||
@@ -24,24 +96,18 @@ class Execution(
|
||||
operator fun <T> Argument.Single<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T = arguments.single { it.name == name }
|
||||
): T = arguments.single { it.name in names }
|
||||
.let { parser.parse(it.value) }
|
||||
|
||||
operator fun <T> Argument.SingleOrElse<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T = arguments.singleOrNull { it.name == name }
|
||||
): T = arguments.singleOrNull { it.name in names }
|
||||
?.let { parser.parse(it.value) } ?: other
|
||||
|
||||
operator fun <T> Argument.SingleOrNull<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T? = arguments.singleOrNull { it.name == name }
|
||||
): T? = arguments.singleOrNull { it.name in names }
|
||||
?.let { parser.parse(it.value) }
|
||||
|
||||
operator fun <T> Argument.Multiple<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): List<T> = arguments.filter { it.name == name }
|
||||
.map { parser.parse(it.value) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user