major refactor
This commit is contained in:
@@ -1,16 +1,30 @@
|
||||
package com.infendro.cli
|
||||
|
||||
import com.infendro.cli.argument.Argument.*
|
||||
import com.infendro.cli.command.Command
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Execution(
|
||||
val arguments: List<Argument>,
|
||||
val commands: List<Cmd>,
|
||||
val arguments: List<Arg>,
|
||||
) {
|
||||
class Argument(
|
||||
class Cmd(
|
||||
val name: String,
|
||||
)
|
||||
|
||||
class Arg(
|
||||
val name: String,
|
||||
val value: String?,
|
||||
)
|
||||
|
||||
operator fun Command.Key.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): String {
|
||||
return commands[index]
|
||||
.name
|
||||
}
|
||||
|
||||
operator fun <T> Single<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
|
||||
@@ -42,4 +42,10 @@ sealed class Argument<T> {
|
||||
|
||||
abstract val parser: Parser<T>
|
||||
}
|
||||
|
||||
interface Parser<T> {
|
||||
fun parse(
|
||||
text: String?,
|
||||
): T
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.infendro.cli.argument
|
||||
|
||||
import com.infendro.cli.Command
|
||||
import com.infendro.cli.command.Command
|
||||
|
||||
private object BooleanParser : Parser<Boolean> {
|
||||
private object BooleanParser : Argument.Parser<Boolean> {
|
||||
override fun parse(
|
||||
text: String?,
|
||||
): Boolean {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.infendro.cli.argument
|
||||
|
||||
import com.infendro.cli.Command
|
||||
import com.infendro.cli.command.Command
|
||||
|
||||
private object IntParser : Parser<Int> {
|
||||
private object IntParser : Argument.Parser<Int> {
|
||||
override fun parse(
|
||||
text: String?,
|
||||
): Int {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.infendro.cli.argument
|
||||
|
||||
interface Parser<T> {
|
||||
fun parse(
|
||||
text: String?,
|
||||
): T
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.infendro.cli.argument
|
||||
|
||||
import com.infendro.cli.Command
|
||||
import com.infendro.cli.command.Command
|
||||
|
||||
private object StringParser : Parser<String> {
|
||||
private object StringParser : Argument.Parser<String> {
|
||||
override fun parse(
|
||||
text: String?,
|
||||
): String {
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
package com.infendro.cli
|
||||
package com.infendro.cli.command
|
||||
|
||||
import com.infendro.cli.Regex.ARGUMENT
|
||||
import com.infendro.cli.Regex.ARGUMENT_NAME
|
||||
import com.infendro.cli.Regex.COMMAND_NAME
|
||||
import com.infendro.cli.Execution
|
||||
import com.infendro.cli.argument.Argument
|
||||
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 name: String,
|
||||
private val index: Int,
|
||||
private val name: String?,
|
||||
private val commands: List<Command>,
|
||||
private val arguments: List<Argument<*>>,
|
||||
private val block: Execution.() -> Unit,
|
||||
) {
|
||||
private val nextIndex: Int
|
||||
get() = index + 1
|
||||
|
||||
fun run(
|
||||
args: Array<String>,
|
||||
) {
|
||||
@@ -24,11 +29,11 @@ class Command private constructor(
|
||||
.find { (_, value) -> value.startsWith("--") }
|
||||
?.index ?: args.size
|
||||
|
||||
val c = args.slice(0..<index)
|
||||
val a = args.slice(index..<args.size)
|
||||
val cmd = args.slice(0..<index)
|
||||
val arg = args.slice(index..<args.size)
|
||||
|
||||
try {
|
||||
run(c, a)
|
||||
run(cmd, arg)
|
||||
} catch (e: UnknownCommandException) {
|
||||
println("""unknown command "${e.command}"""")
|
||||
} catch (e: InvalidArgumentException) {
|
||||
@@ -45,33 +50,35 @@ class Command private constructor(
|
||||
}
|
||||
|
||||
private fun run(
|
||||
cmd: List<String>, args: List<String>,
|
||||
cmd: List<String>,
|
||||
arg: List<String>,
|
||||
) {
|
||||
if (cmd.isEmpty()) {
|
||||
val arguments = parseArguments(args)
|
||||
execute(arguments)
|
||||
if (index == cmd.lastIndex) {
|
||||
Execution(
|
||||
parseCmd(cmd),
|
||||
parseArg(arg),
|
||||
).block()
|
||||
} else {
|
||||
val command = commands.find { it.name == cmd[0] }
|
||||
?: throw UnknownCommandException(cmd[0])
|
||||
command.run(cmd.drop(1), args)
|
||||
val command = commands.firstOrNull() { it.name == cmd[nextIndex] || it.name == null }
|
||||
?: throw UnknownCommandException(cmd[nextIndex])
|
||||
command.run(cmd, arg)
|
||||
}
|
||||
}
|
||||
|
||||
private fun execute(
|
||||
arguments: List<Execution.Argument>,
|
||||
) {
|
||||
val execution = Execution(arguments)
|
||||
execution.block()
|
||||
private fun parseCmd(
|
||||
cmd: List<String>,
|
||||
): List<Execution.Cmd> {
|
||||
return cmd.map { Execution.Cmd(it) }
|
||||
}
|
||||
|
||||
private fun parseArguments(
|
||||
args: List<String>,
|
||||
): List<Execution.Argument> {
|
||||
val result = mutableListOf<Execution.Argument>()
|
||||
private fun parseArg(
|
||||
arg: List<String>,
|
||||
): List<Execution.Arg> {
|
||||
val result = mutableListOf<Execution.Arg>()
|
||||
|
||||
for (arg in args) {
|
||||
val match = ARGUMENT.matchEntire(arg)
|
||||
?: throw InvalidArgumentException(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
|
||||
@@ -79,7 +86,7 @@ class Command private constructor(
|
||||
if (arguments.none { it.name == name })
|
||||
throw UnknownArgumentException(name)
|
||||
|
||||
result += Execution.Argument(name, value)
|
||||
result += Execution.Arg(name, value)
|
||||
}
|
||||
|
||||
arguments
|
||||
@@ -92,26 +99,31 @@ class Command private constructor(
|
||||
return result
|
||||
}
|
||||
|
||||
class Builder(
|
||||
private val name: String = "",
|
||||
class Builder internal constructor(
|
||||
private val index: Int,
|
||||
private val name: String?,
|
||||
) {
|
||||
private val commands = mutableListOf<Command>()
|
||||
private val arguments = mutableListOf<Argument<*>>()
|
||||
private lateinit var block: Execution.() -> Unit
|
||||
|
||||
private val nextIndex: Int
|
||||
get() = index + 1
|
||||
|
||||
fun command(
|
||||
name: String,
|
||||
block: Builder.() -> Unit,
|
||||
name: String? = null,
|
||||
block: Builder.(Key) -> Unit,
|
||||
) {
|
||||
if (!COMMAND_NAME.matches(name))
|
||||
if (name != null && !COMMAND_NAME.matches(name))
|
||||
throw IllegalCommandNameException(name)
|
||||
|
||||
if (commands.any { it.name == name })
|
||||
throw DuplicateCommandException(name)
|
||||
|
||||
val builder = Builder(name)
|
||||
builder.block()
|
||||
val builder = Builder(nextIndex, name)
|
||||
builder.block(Key(nextIndex))
|
||||
val command = builder.build()
|
||||
|
||||
commands.add(command)
|
||||
}
|
||||
|
||||
@@ -148,15 +160,19 @@ class Command private constructor(
|
||||
if (!this::block.isInitialized)
|
||||
throw NoExecuteException()
|
||||
|
||||
return Command(name, commands, arguments, block)
|
||||
return Command(index, name, commands, arguments, block)
|
||||
}
|
||||
}
|
||||
|
||||
class Key(
|
||||
internal val index: Int,
|
||||
)
|
||||
}
|
||||
|
||||
fun cli(
|
||||
block: Command.Builder.() -> Unit,
|
||||
): Command {
|
||||
val builder = Command.Builder()
|
||||
val builder = Command.Builder(-1, null)
|
||||
builder.block()
|
||||
return builder.build()
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.infendro.cli.exception.build
|
||||
|
||||
class DuplicateCommandException(
|
||||
val command: String,
|
||||
val command: String?,
|
||||
) : Exception()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.infendro.cli
|
||||
package com.infendro.cli.util
|
||||
|
||||
internal object Regex {
|
||||
val COMMAND_NAME = """[a-z]+(?:-[a-z]+)*""".toRegex()
|
||||
val ARGUMENT_NAME = """[a-z]+(?:[\-.][a-z]+)*""".toRegex()
|
||||
val ARGUMENT = """--(?<name>[a-z]+(?:[\-.][a-z]+)*)(?:=(?<value>.*))?""".toRegex()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user