1.4.0 release #9
@@ -1,44 +0,0 @@
|
|||||||
package com.infendro.cli.argument
|
|
||||||
|
|
||||||
import com.infendro.cli.exception.build.NoArgumentNameException
|
|
||||||
|
|
||||||
sealed class Argument<T>(
|
|
||||||
val names: List<String>,
|
|
||||||
val parser: Parser<T>,
|
|
||||||
) {
|
|
||||||
abstract val required: Boolean
|
|
||||||
|
|
||||||
val name: String
|
|
||||||
get() = names[0]
|
|
||||||
|
|
||||||
init {
|
|
||||||
if (names.isEmpty())
|
|
||||||
throw NoArgumentNameException()
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Parser<T> {
|
|
||||||
fun parse(text: String?): T
|
|
||||||
}
|
|
||||||
|
|
||||||
class Single<T>(
|
|
||||||
names: List<String>,
|
|
||||||
parser: Parser<T>,
|
|
||||||
) : Argument<T>(names, parser) {
|
|
||||||
override val required: Boolean = true
|
|
||||||
}
|
|
||||||
|
|
||||||
class SingleOrElse<T>(
|
|
||||||
names: List<String>,
|
|
||||||
parser: Parser<T>,
|
|
||||||
val other: T,
|
|
||||||
) : Argument<T>(names, parser) {
|
|
||||||
override val required: Boolean = false
|
|
||||||
}
|
|
||||||
|
|
||||||
class SingleOrNull<T>(
|
|
||||||
names: List<String>,
|
|
||||||
parser: Parser<T>,
|
|
||||||
) : Argument<T>(names, parser) {
|
|
||||||
override val required: Boolean = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package com.infendro.cli.argument
|
|
||||||
|
|
||||||
import com.infendro.cli.command.Command
|
|
||||||
import com.infendro.cli.exception.run.ParseException
|
|
||||||
|
|
||||||
object BooleanParser : Argument.Parser<Boolean> {
|
|
||||||
override fun parse(text: String?): Boolean = try {
|
|
||||||
text?.toBooleanStrict() ?: true
|
|
||||||
} catch (_: IllegalArgumentException) {
|
|
||||||
throw ParseException(text, "Boolean")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun boolean(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), BooleanParser)
|
|
||||||
|
|
||||||
fun booleanOrElse(vararg names: String, other: Boolean) =
|
|
||||||
Argument.SingleOrElse(names.asList(), BooleanParser, other)
|
|
||||||
|
|
||||||
fun booleanOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), BooleanParser)
|
|
||||||
|
|
||||||
fun Command.Builder.boolean(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), BooleanParser).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.booleanOrElse(vararg names: String, other: Boolean) =
|
|
||||||
Argument.SingleOrElse(names.asList(), BooleanParser, other).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.booleanOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), BooleanParser).also { argument(it) }
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package com.infendro.cli.argument
|
|
||||||
|
|
||||||
import com.infendro.cli.command.Command
|
|
||||||
import com.infendro.cli.exception.run.ParseException
|
|
||||||
|
|
||||||
object IntParser : Argument.Parser<Int> {
|
|
||||||
override fun parse(text: String?): Int = try {
|
|
||||||
text!!.toInt()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
when (e) {
|
|
||||||
is NullPointerException, is IllegalArgumentException -> throw ParseException(text, "Int")
|
|
||||||
else -> throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun int(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), IntParser)
|
|
||||||
|
|
||||||
fun intOrElse(vararg names: String, other: Int) =
|
|
||||||
Argument.SingleOrElse(names.asList(), IntParser, other)
|
|
||||||
|
|
||||||
fun intOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), IntParser)
|
|
||||||
|
|
||||||
fun Command.Builder.int(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), IntParser).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.intOrElse(vararg names: String, other: Int) =
|
|
||||||
Argument.SingleOrElse(names.asList(), IntParser, other).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.intOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), IntParser).also { argument(it) }
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.infendro.cli.argument
|
|
||||||
|
|
||||||
import com.infendro.cli.command.Command
|
|
||||||
import com.infendro.cli.exception.run.ParseException
|
|
||||||
|
|
||||||
object StringParser : Argument.Parser<String> {
|
|
||||||
override fun parse(text: String?): String =
|
|
||||||
text ?: throw ParseException(text, "String")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun string(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), StringParser)
|
|
||||||
|
|
||||||
fun stringOrElse(vararg names: String, other: String) =
|
|
||||||
Argument.SingleOrElse(names.asList(), StringParser, other)
|
|
||||||
|
|
||||||
fun stringOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), StringParser)
|
|
||||||
|
|
||||||
fun Command.Builder.string(vararg names: String) =
|
|
||||||
Argument.Single(names.asList(), StringParser).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.stringOrElse(vararg names: String, other: String) =
|
|
||||||
Argument.SingleOrElse(names.asList(), StringParser, other).also { argument(it) }
|
|
||||||
|
|
||||||
fun Command.Builder.stringOrNull(vararg names: String) =
|
|
||||||
Argument.SingleOrNull(names.asList(), StringParser).also { argument(it) }
|
|
||||||
79
src/commonMain/kotlin/com/infendro/cli/command/Argument.kt
Normal file
79
src/commonMain/kotlin/com/infendro/cli/command/Argument.kt
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package com.infendro.cli.command
|
||||||
|
|
||||||
|
import com.infendro.cli.parser.BooleanParser
|
||||||
|
import com.infendro.cli.parser.IntParser
|
||||||
|
import com.infendro.cli.parser.Parser
|
||||||
|
import com.infendro.cli.parser.StringParser
|
||||||
|
|
||||||
|
sealed class Argument<T>(
|
||||||
|
val parser: Parser<T>,
|
||||||
|
) {
|
||||||
|
abstract val required: Boolean
|
||||||
|
|
||||||
|
class Required<T>(
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Argument<T>(parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = true
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrElse<T>(
|
||||||
|
val other: T,
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Argument<T>(parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrNull<T>(
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Argument<T>(parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
class Variable<T>(
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Argument<T>(parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun string() =
|
||||||
|
Required(StringParser)
|
||||||
|
|
||||||
|
fun stringOrElse(other: String) =
|
||||||
|
OrElse(other, StringParser)
|
||||||
|
|
||||||
|
fun stringOrNull() =
|
||||||
|
OrNull(StringParser)
|
||||||
|
|
||||||
|
fun strings() =
|
||||||
|
Variable(StringParser)
|
||||||
|
|
||||||
|
fun int() =
|
||||||
|
Required(IntParser)
|
||||||
|
|
||||||
|
fun intOrElse(other: Int) =
|
||||||
|
OrElse(other, IntParser)
|
||||||
|
|
||||||
|
fun intOrNull() =
|
||||||
|
OrNull(IntParser)
|
||||||
|
|
||||||
|
fun ints() =
|
||||||
|
Variable(IntParser)
|
||||||
|
|
||||||
|
fun boolean() =
|
||||||
|
Required(BooleanParser)
|
||||||
|
|
||||||
|
fun booleanOrElse(other: Boolean) =
|
||||||
|
OrElse(other, BooleanParser)
|
||||||
|
|
||||||
|
fun booleanOrNull() =
|
||||||
|
OrNull(BooleanParser)
|
||||||
|
|
||||||
|
fun booleans() =
|
||||||
|
Variable(BooleanParser)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,35 +1,22 @@
|
|||||||
package com.infendro.cli.command
|
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.build.*
|
||||||
import com.infendro.cli.exception.run.UnknownCommandException
|
import com.infendro.cli.exception.run.CliException
|
||||||
import com.infendro.cli.util.Regex.ARGUMENT_NAME
|
import com.infendro.cli.util.Regex.COMMAND
|
||||||
import com.infendro.cli.util.Regex.COMMAND_NAME
|
import com.infendro.cli.util.Regex.OPTION
|
||||||
|
|
||||||
class Command private constructor(
|
class Command private constructor(
|
||||||
val name: String?,
|
val name: String?,
|
||||||
val commands: List<Command>,
|
val commands: List<Command>,
|
||||||
|
val options: List<Option<*>>,
|
||||||
val arguments: List<Argument<*>>,
|
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 {
|
fun run(args: Array<String>) = try {
|
||||||
val index = args.withIndex()
|
Parser.parse(this, args).execute()
|
||||||
.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()
|
|
||||||
} catch (e: CliException) {
|
} catch (e: CliException) {
|
||||||
println(e.message)
|
println(e.message)
|
||||||
}
|
}
|
||||||
@@ -40,53 +27,78 @@ class Command private constructor(
|
|||||||
) {
|
) {
|
||||||
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 val options = mutableListOf<Option<*>>()
|
||||||
|
private lateinit var _execute: Context.() -> Unit
|
||||||
|
|
||||||
private val nextIndex: Int
|
private val nextIndex: Int
|
||||||
get() = index + 1
|
get() = index + 1
|
||||||
|
|
||||||
fun command(name: String? = null, block: Builder.(Key) -> Unit) {
|
fun command(name: String? = null, block: Builder.(Key) -> Unit) {
|
||||||
if (name != null && !name.matches(COMMAND_NAME))
|
// validate
|
||||||
throw IllegalCommandNameException(name)
|
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 })
|
commands += Builder(nextIndex, name).also {
|
||||||
throw DuplicateCommandException(name)
|
val key = Key(nextIndex)
|
||||||
|
it.block(key)
|
||||||
val builder = Builder(nextIndex, name)
|
}.build()
|
||||||
val key = Key(nextIndex)
|
|
||||||
builder.block(key)
|
|
||||||
val command = builder.build()
|
|
||||||
|
|
||||||
commands.add(command)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun argument(argument: Argument<*>) {
|
fun argument(argument: Argument<*>) {
|
||||||
for (name in argument.names) {
|
// validate
|
||||||
if (!name.matches(ARGUMENT_NAME))
|
val last = arguments.lastOrNull()
|
||||||
throw IllegalArgumentNameException(name)
|
when {
|
||||||
|
last is Argument.Variable -> throw ArgumentOrderException()
|
||||||
if (arguments.any { name in it.names })
|
argument.required && last?.required == false -> throw ArgumentOrderException()
|
||||||
throw DuplicateArgumentException(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
arguments.add(argument)
|
arguments += argument
|
||||||
}
|
}
|
||||||
|
|
||||||
fun arguments(vararg arguments: Argument<*>) =
|
fun <T : Argument<*>> T.register() = also(::argument)
|
||||||
arguments.forEach(::argument)
|
|
||||||
|
|
||||||
fun execute(block: Execution.() -> Unit) {
|
fun arguments(vararg arguments: Argument<*>) = arguments.forEach(::argument)
|
||||||
if (this::block.isInitialized)
|
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()
|
throw DuplicateExecuteException()
|
||||||
|
|
||||||
this.block = block
|
_execute = block
|
||||||
}
|
}
|
||||||
|
|
||||||
fun build(): Command {
|
fun build(): Command {
|
||||||
if (!this::block.isInitialized)
|
// validate
|
||||||
throw NoExecuteException()
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
56
src/commonMain/kotlin/com/infendro/cli/command/Context.kt
Normal file
56
src/commonMain/kotlin/com/infendro/cli/command/Context.kt
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package com.infendro.cli.command
|
||||||
|
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
class Context internal constructor(
|
||||||
|
val command: Command,
|
||||||
|
val commands: List<Cmd>,
|
||||||
|
val options: List<Opt>,
|
||||||
|
val arguments: List<Arg>,
|
||||||
|
) {
|
||||||
|
class Cmd(
|
||||||
|
val name: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
class Arg(
|
||||||
|
val value: Any,
|
||||||
|
)
|
||||||
|
|
||||||
|
class Opt(
|
||||||
|
val name: String,
|
||||||
|
val value: Any,
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun execute() = (command.execute)()
|
||||||
|
|
||||||
|
operator fun Command.Key.getValue(thisRef: Any?, property: KProperty<*>): String = commands[index].name
|
||||||
|
|
||||||
|
private val <T> Argument<T>.index: Int
|
||||||
|
get() = command.arguments.indexOf(this)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
private val <T> Argument<T>.value: T?
|
||||||
|
get() = arguments.getOrNull(index)?.value as? T
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
private val <T> Argument.Variable<T>.values: List<T>
|
||||||
|
get() = arguments.drop(index).map { it.value as T }
|
||||||
|
|
||||||
|
operator fun <T> Argument.Required<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value!!
|
||||||
|
operator fun <T> Argument.OrElse<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value ?: other
|
||||||
|
operator fun <T> Argument.OrNull<T>.getValue(thisRef: Any?, property: KProperty<*>): T? = value
|
||||||
|
operator fun <T> Argument.Variable<T>.getValue(thisRef: Any?, property: KProperty<*>): List<T> = values
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
private val <T> Option<T>.value: T?
|
||||||
|
get() = options.firstOrNull { it.name in names }?.value as? T
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
private val <T> Option.Variable<T>.values: List<T>
|
||||||
|
get() = options.filter { it.name in names }.map { it.value as T }
|
||||||
|
|
||||||
|
operator fun <T> Option.Required<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value!!
|
||||||
|
operator fun <T> Option.OrElse<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value ?: other
|
||||||
|
operator fun <T> Option.OrNull<T>.getValue(thisRef: Any?, property: KProperty<*>): T? = value
|
||||||
|
operator fun <T> Option.Variable<T>.getValue(thisRef: Any?, property: KProperty<*>): List<T> = values
|
||||||
|
}
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
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 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: Any,
|
|
||||||
) {
|
|
||||||
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) {
|
|
||||||
val argument = command.arguments.firstOrNull { name in it.names }
|
|
||||||
?: throw UnknownArgumentException(name)
|
|
||||||
|
|
||||||
val value = argument.parser.parse(value) as Any
|
|
||||||
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?, property: KProperty<*>): String = commands[index].name
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
private fun <T> Argument<T>.getValue(): T? = arguments.firstOrNull { it.name in names }?.value as? T
|
|
||||||
operator fun <T> Argument.Single<T>.getValue(thisRef: Any?, property: KProperty<*>): T = getValue()!!
|
|
||||||
operator fun <T> Argument.SingleOrElse<T>.getValue(thisRef: Any?, property: KProperty<*>): T = getValue() ?: other
|
|
||||||
operator fun <T> Argument.SingleOrNull<T>.getValue(thisRef: Any?, property: KProperty<*>): T? = getValue()
|
|
||||||
}
|
|
||||||
93
src/commonMain/kotlin/com/infendro/cli/command/Option.kt
Normal file
93
src/commonMain/kotlin/com/infendro/cli/command/Option.kt
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package com.infendro.cli.command
|
||||||
|
|
||||||
|
import com.infendro.cli.exception.build.NoOptionNameException
|
||||||
|
import com.infendro.cli.parser.BooleanParser
|
||||||
|
import com.infendro.cli.parser.IntParser
|
||||||
|
import com.infendro.cli.parser.Parser
|
||||||
|
import com.infendro.cli.parser.StringParser
|
||||||
|
|
||||||
|
sealed class Option<T>(
|
||||||
|
val names: List<String>,
|
||||||
|
val parser: Parser<T>,
|
||||||
|
) {
|
||||||
|
val name: String
|
||||||
|
get() = names[0]
|
||||||
|
|
||||||
|
abstract val required: Boolean
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (names.isEmpty())
|
||||||
|
throw NoOptionNameException()
|
||||||
|
}
|
||||||
|
|
||||||
|
class Required<T>(
|
||||||
|
names: List<String>,
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Option<T>(names, parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = true
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrElse<T>(
|
||||||
|
names: List<String>,
|
||||||
|
val other: T,
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Option<T>(names, parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrNull<T>(
|
||||||
|
names: List<String>,
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Option<T>(names, parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
class Variable<T>(
|
||||||
|
names: List<String>,
|
||||||
|
parser: Parser<T>,
|
||||||
|
) : Option<T>(names, parser) {
|
||||||
|
override val required: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun string(vararg names: String) =
|
||||||
|
Required(names.asList(), StringParser)
|
||||||
|
|
||||||
|
fun stringOrElse(vararg names: String, other: String) =
|
||||||
|
OrElse(names.asList(), other, StringParser)
|
||||||
|
|
||||||
|
fun stringOrNull(vararg names: String) =
|
||||||
|
OrNull(names.asList(), StringParser)
|
||||||
|
|
||||||
|
fun strings(vararg names: String) =
|
||||||
|
Variable(names.asList(), StringParser)
|
||||||
|
|
||||||
|
fun int(vararg names: String) =
|
||||||
|
Required(names.asList(), IntParser)
|
||||||
|
|
||||||
|
fun intOrElse(vararg names: String, other: Int) =
|
||||||
|
OrElse(names.asList(), other, IntParser)
|
||||||
|
|
||||||
|
fun intOrNull(vararg names: String) =
|
||||||
|
OrNull(names.asList(), IntParser)
|
||||||
|
|
||||||
|
fun ints(vararg names: String) =
|
||||||
|
Variable(names.asList(), IntParser)
|
||||||
|
|
||||||
|
fun boolean(vararg names: String) =
|
||||||
|
Required(names.asList(), BooleanParser)
|
||||||
|
|
||||||
|
fun booleanOrElse(vararg names: String, other: Boolean) =
|
||||||
|
OrElse(names.asList(), other, BooleanParser)
|
||||||
|
|
||||||
|
fun booleanOrNull(vararg names: String) =
|
||||||
|
OrNull(names.asList(), BooleanParser)
|
||||||
|
|
||||||
|
fun booleans(vararg names: String) =
|
||||||
|
Variable(names.asList(), BooleanParser)
|
||||||
|
}
|
||||||
|
}
|
||||||
120
src/commonMain/kotlin/com/infendro/cli/command/Parser.kt
Normal file
120
src/commonMain/kotlin/com/infendro/cli/command/Parser.kt
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package com.infendro.cli.command
|
||||||
|
|
||||||
|
import com.infendro.cli.command.Context.*
|
||||||
|
import com.infendro.cli.exception.run.*
|
||||||
|
|
||||||
|
internal object Parser {
|
||||||
|
fun parse(root: Command, args: Array<String>): Context {
|
||||||
|
var command = root
|
||||||
|
val cmd = mutableListOf<Cmd>()
|
||||||
|
val arg = mutableListOf<Arg>()
|
||||||
|
val opt = mutableListOf<Opt>()
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
|
||||||
|
// commands
|
||||||
|
while (i < args.size) {
|
||||||
|
val current = args[i]
|
||||||
|
|
||||||
|
if (current.startsWith("-"))
|
||||||
|
break
|
||||||
|
|
||||||
|
val c = command.commands.firstOrNull { it.name == current || it.fallback }
|
||||||
|
if (c == null) {
|
||||||
|
when {
|
||||||
|
command.arguments.isEmpty() -> throw UnknownCommandException(current)
|
||||||
|
else -> break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
command = c
|
||||||
|
cmd += Cmd(current)
|
||||||
|
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
// arguments and options
|
||||||
|
var endOfOptions = false
|
||||||
|
var argumentIndex = 0
|
||||||
|
while (i < args.size) {
|
||||||
|
val dashes = args[i].takeWhile { it == '-' }.count()
|
||||||
|
val current = args[i].drop(dashes)
|
||||||
|
|
||||||
|
when {
|
||||||
|
endOfOptions || dashes == 0 -> {
|
||||||
|
val argument = command.arguments.getOrNull(argumentIndex)
|
||||||
|
?: throw TooManyArgumentsException()
|
||||||
|
|
||||||
|
if (argument !is Argument.Variable)
|
||||||
|
argumentIndex++
|
||||||
|
|
||||||
|
val value = argument.parser.parse(current) as Any
|
||||||
|
arg += Arg(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
dashes in 1..2 -> {
|
||||||
|
if (dashes == 2 && current.isEmpty()) {
|
||||||
|
endOfOptions = true
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val (name, value) = when {
|
||||||
|
current.contains('=') -> {
|
||||||
|
val (name, value) = current.split('=', limit = 2)
|
||||||
|
name to value
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
val next = args.getOrNull(i + 1)
|
||||||
|
if (next != null && !next.startsWith('-')) {
|
||||||
|
i++
|
||||||
|
current to next
|
||||||
|
} else {
|
||||||
|
current to null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val names = when {
|
||||||
|
dashes == 1 -> name.map { "$it" }
|
||||||
|
else -> listOf(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (name in names) {
|
||||||
|
val option = command.options.firstOrNull { name in it.names }
|
||||||
|
?: throw UnknownOptionException(name)
|
||||||
|
|
||||||
|
val value = option.parser.parse(value) as Any
|
||||||
|
opt += Opt(name, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
when {
|
||||||
|
args[i].contains('=') -> {
|
||||||
|
val (option) = args[i].split('=', limit = 2)
|
||||||
|
throw MalformedOptionException(option)
|
||||||
|
}
|
||||||
|
else -> throw MalformedOptionException(args[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg.size < command.arguments.count { it is Argument.Required })
|
||||||
|
throw MissingArgumentException()
|
||||||
|
|
||||||
|
for (option in command.options) {
|
||||||
|
val count = opt.count { it.name in option.names }
|
||||||
|
when {
|
||||||
|
option is Option.Required && count == 0 -> throw MissingOptionException(option.name)
|
||||||
|
option !is Option.Variable && count > 1 -> throw DuplicateOptionException(option.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Context(command, cmd, opt, arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package com.infendro.cli.exception
|
|
||||||
|
|
||||||
abstract class CliException : Exception()
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
|
class ArgumentOrderException : Exception()
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package com.infendro.cli.exception.build
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
class NoExecuteException : Exception()
|
class CommandOrderException : Exception()
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.infendro.cli.exception.build
|
|
||||||
|
|
||||||
class DuplicateArgumentException(
|
|
||||||
val argument: String,
|
|
||||||
) : Exception()
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package com.infendro.cli.exception.build
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
class IllegalArgumentNameException(
|
class DuplicateOptionException(
|
||||||
val name: String,
|
val name: String,
|
||||||
) : Exception()
|
) : Exception()
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
|
class IllegalOptionNameException(
|
||||||
|
val name: String,
|
||||||
|
) : Exception()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
|
class InvalidFallbackException : Exception()
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package com.infendro.cli.exception.build
|
|
||||||
|
|
||||||
class NoArgumentNameException : Exception()
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.infendro.cli.exception.build
|
||||||
|
|
||||||
|
class NoOptionNameException : Exception()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
abstract class CliException : Exception()
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.cli.exception.run
|
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
|
||||||
|
|
||||||
class DuplicateArgumentException(
|
|
||||||
val argument: String,
|
|
||||||
) : CliException() {
|
|
||||||
override val message: String
|
|
||||||
get() = """duplicate argument "$argument""""
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class DuplicateOptionException(
|
||||||
|
val option: String,
|
||||||
|
) : CliException() {
|
||||||
|
override val message: String
|
||||||
|
get() = """duplicate option "$option""""
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.cli.exception.run
|
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
|
||||||
|
|
||||||
class MalformedArgumentException(
|
|
||||||
val argument: String,
|
|
||||||
) : CliException() {
|
|
||||||
override val message: String
|
|
||||||
get() = """malformed argument "$argument""""
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class MalformedOptionException(
|
||||||
|
val option: String,
|
||||||
|
) : CliException() {
|
||||||
|
override val message: String
|
||||||
|
get() = """malformed option "$option""""
|
||||||
|
}
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
package com.infendro.cli.exception.run
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
class MissingArgumentException : CliException() {
|
||||||
|
|
||||||
class MissingArgumentException(
|
|
||||||
val argument: String,
|
|
||||||
) : CliException() {
|
|
||||||
override val message: String
|
override val message: String
|
||||||
get() = """missing argument "$argument""""
|
get() = """missing argument"""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class MissingOptionException(
|
||||||
|
val option: String,
|
||||||
|
) : CliException() {
|
||||||
|
override val message: String
|
||||||
|
get() = """missing option "$option""""
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.infendro.cli.exception.run
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
|
||||||
|
|
||||||
class ParseException(
|
class ParseException(
|
||||||
val text: String?,
|
val text: String?,
|
||||||
val type: String,
|
val type: String,
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class TooManyArgumentsException : CliException() {
|
||||||
|
override val message: String
|
||||||
|
get() = """too many arguments"""
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.cli.exception.run
|
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
|
||||||
|
|
||||||
class UnknownArgumentException(
|
|
||||||
val argument: String,
|
|
||||||
) : CliException() {
|
|
||||||
override val message: String
|
|
||||||
get() = """unknown argument "$argument""""
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.infendro.cli.exception.run
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
import com.infendro.cli.exception.CliException
|
|
||||||
|
|
||||||
class UnknownCommandException(
|
class UnknownCommandException(
|
||||||
val command: String,
|
val command: String,
|
||||||
) : CliException() {
|
) : CliException() {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class UnknownOptionException(
|
||||||
|
val option: String,
|
||||||
|
) : CliException() {
|
||||||
|
override val message: String
|
||||||
|
get() = """unknown option "$option""""
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.infendro.cli.parser
|
||||||
|
|
||||||
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
|
object BooleanParser : Parser<Boolean> {
|
||||||
|
override fun parse(text: String?): Boolean = when (text) {
|
||||||
|
"true", "t", null -> true
|
||||||
|
"false", "f" -> false
|
||||||
|
else -> throw ParseException(text, "Boolean")
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/commonMain/kotlin/com/infendro/cli/parser/IntParser.kt
Normal file
11
src/commonMain/kotlin/com/infendro/cli/parser/IntParser.kt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.infendro.cli.parser
|
||||||
|
|
||||||
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
|
object IntParser : Parser<Int> {
|
||||||
|
override fun parse(text: String?): Int = try {
|
||||||
|
text!!.toInt()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
throw ParseException(text, "Int")
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/commonMain/kotlin/com/infendro/cli/parser/Parser.kt
Normal file
5
src/commonMain/kotlin/com/infendro/cli/parser/Parser.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.cli.parser
|
||||||
|
|
||||||
|
interface Parser<T> {
|
||||||
|
fun parse(text: String?): T
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.infendro.cli.parser
|
||||||
|
|
||||||
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
|
object StringParser : Parser<String> {
|
||||||
|
override fun parse(text: String?): String = text ?: throw ParseException(text, "String")
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.infendro.cli.util
|
package com.infendro.cli.util
|
||||||
|
|
||||||
internal object Regex {
|
internal object Regex {
|
||||||
val COMMAND_NAME = Regex("""[a-z]+(?:-[a-z]+)*""")
|
private const val L = "[a-zA-Z]"
|
||||||
val ARGUMENT_NAME = Regex("""[a-z]+(?:[\-.][a-z]+)*""")
|
private const val AN = """[a-zA-Z0-9]"""
|
||||||
|
val COMMAND = Regex("""${L}${AN}*(?:-${AN}+)*""")
|
||||||
|
val OPTION = Regex("""${L}${AN}*(?:[\-.]${AN}+)*""")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user