improve argument/option declaration
This commit is contained in:
@@ -41,28 +41,17 @@ sealed class Argument<T : Any>(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun string() =
|
||||
Required(StringParser)
|
||||
fun string() = argument(StringParser)
|
||||
fun int() = argument(IntParser)
|
||||
fun long() = argument(LongParser)
|
||||
fun float() = argument(FloatParser)
|
||||
fun double() = argument(DoubleParser)
|
||||
fun boolean() = argument(BooleanParser)
|
||||
|
||||
fun int() =
|
||||
Required(IntParser)
|
||||
|
||||
fun long() =
|
||||
Required(LongParser)
|
||||
|
||||
fun float() =
|
||||
Required(FloatParser)
|
||||
|
||||
fun double() =
|
||||
Required(DoubleParser)
|
||||
|
||||
fun boolean() =
|
||||
Required(BooleanParser)
|
||||
|
||||
inline fun <reified T : Enum<T>> enum() =
|
||||
Required(enumParser<T>())
|
||||
|
||||
fun regex() =
|
||||
Required(RegexParser)
|
||||
inline fun <reified T : Enum<T>> enum() = argument(enumParser<T>())
|
||||
fun regex() = argument(RegexParser)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> argument(parser: Parser<T>) =
|
||||
Argument.Required(parser)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.infendro.cli.command
|
||||
|
||||
import com.infendro.cli.Dsl
|
||||
import com.infendro.cli.command.context.Context
|
||||
import com.infendro.cli.command.context.Parser
|
||||
import com.infendro.cli.exception.build.*
|
||||
import com.infendro.cli.exception.run.CliException
|
||||
import com.infendro.cli.util.Regex.COMMAND
|
||||
import com.infendro.cli.util.Regex.OPTION
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Command private constructor(
|
||||
val name: String?,
|
||||
@@ -23,6 +25,7 @@ class Command private constructor(
|
||||
println(e.message)
|
||||
}
|
||||
|
||||
@Dsl
|
||||
class Builder internal constructor(
|
||||
private val level: Int,
|
||||
private val name: String?,
|
||||
@@ -68,10 +71,10 @@ class Command private constructor(
|
||||
arguments += argument
|
||||
}
|
||||
|
||||
fun <T : Argument<*>> T.register() = also(::argument)
|
||||
|
||||
fun arguments(vararg arguments: Argument<*>) = arguments.forEach(::argument)
|
||||
fun <T : Argument<*>> Array<T>.register() = also(::arguments)
|
||||
|
||||
operator fun <T : Argument<*>> T.provideDelegate(thisRef: Nothing?, property: KProperty<*>) = also(::argument)
|
||||
operator fun <T : Argument<*>> T.getValue(thisRef: Nothing?, property: KProperty<*>) = this
|
||||
|
||||
fun option(option: Option<*>) {
|
||||
// validate
|
||||
@@ -85,10 +88,10 @@ class Command private constructor(
|
||||
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)
|
||||
|
||||
operator fun <T : Option<*>> T.provideDelegate(thisRef: Nothing?, property: KProperty<*>) = also(::option)
|
||||
operator fun <T : Option<*>> T.getValue(thisRef: Nothing?, property: KProperty<*>) = this
|
||||
|
||||
fun execute(block: Context.() -> Unit) {
|
||||
if (::_execute.isInitialized)
|
||||
|
||||
@@ -84,28 +84,20 @@ sealed class Option<T : Any>(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun string(vararg names: String) =
|
||||
Required(StringParser, names.asList())
|
||||
fun string(vararg names: String) = option(StringParser, *names)
|
||||
fun int(vararg names: String) = option(IntParser, *names)
|
||||
fun long(vararg names: String) = option(LongParser, *names)
|
||||
fun float(vararg names: String) = option(FloatParser, *names)
|
||||
fun double(vararg names: String) = option(DoubleParser, *names)
|
||||
fun boolean(vararg names: String) = option(BooleanParser, true, *names)
|
||||
|
||||
fun int(vararg names: String) =
|
||||
Required(IntParser, names.asList())
|
||||
|
||||
fun long(vararg names: String) =
|
||||
Required(LongParser, names.asList())
|
||||
|
||||
fun float(vararg names: String) =
|
||||
Required(FloatParser, names.asList())
|
||||
|
||||
fun double(vararg names: String) =
|
||||
Required(DoubleParser, names.asList())
|
||||
|
||||
fun boolean(vararg names: String) =
|
||||
Required(BooleanParser, true, names.asList())
|
||||
|
||||
inline fun <reified T : Enum<T>> enum(vararg names: String) =
|
||||
Required(enumParser<T>(), names.asList())
|
||||
|
||||
fun regex(vararg names: String) =
|
||||
Required(RegexParser, names.asList())
|
||||
inline fun <reified T : Enum<T>> enum(vararg names: String) = option(enumParser<T>(), *names)
|
||||
fun regex(vararg names: String) = option(RegexParser, *names)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> option(parser: Parser<T>, vararg names: String) =
|
||||
Option.Required(parser, names.asList())
|
||||
|
||||
fun <T : Any> option(parser: Parser<T>, fallback: T, vararg names: String) =
|
||||
Option.Required(parser, fallback, names.asList())
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.infendro.cli.command.context
|
||||
|
||||
import com.infendro.cli.Dsl
|
||||
import com.infendro.cli.command.Argument
|
||||
import com.infendro.cli.command.Command
|
||||
import com.infendro.cli.command.Option
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
@Dsl
|
||||
class Context internal constructor(
|
||||
val command: Command,
|
||||
val commands: List<Cmd>,
|
||||
|
||||
Reference in New Issue
Block a user