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