Compare commits
2 Commits
86409e9f72
...
8e491751f5
| Author | SHA1 | Date | |
|---|---|---|---|
|
8e491751f5
|
|||
|
0a8e51c7ab
|
4
src/commonMain/kotlin/com/infendro/cli/Dsl.kt
Normal file
4
src/commonMain/kotlin/com/infendro/cli/Dsl.kt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.infendro.cli
|
||||||
|
|
||||||
|
@DslMarker
|
||||||
|
internal annotation class Dsl()
|
||||||
@@ -41,28 +41,16 @@ sealed class Argument<T : Any>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun string() =
|
fun string() = argument(StringParser)
|
||||||
Required(StringParser)
|
fun int() = argument(IntParser)
|
||||||
|
fun long() = argument(LongParser)
|
||||||
|
fun float() = argument(FloatParser)
|
||||||
|
fun double() = argument(DoubleParser)
|
||||||
|
fun boolean() = argument(BooleanParser)
|
||||||
|
|
||||||
fun int() =
|
inline fun <reified T : Enum<T>> enum() = argument(enumParser<T>())
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : Any> argument(parser: Parser<T>) =
|
||||||
|
Argument.Required(parser)
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package com.infendro.cli.command
|
package com.infendro.cli.command
|
||||||
|
|
||||||
|
import com.infendro.cli.Dsl
|
||||||
import com.infendro.cli.command.context.Context
|
import com.infendro.cli.command.context.Context
|
||||||
import com.infendro.cli.command.context.Parser
|
import com.infendro.cli.command.context.Parser
|
||||||
import com.infendro.cli.exception.build.*
|
import com.infendro.cli.exception.build.*
|
||||||
import com.infendro.cli.exception.run.CliException
|
import com.infendro.cli.exception.run.CliException
|
||||||
import com.infendro.cli.util.Regex.COMMAND
|
import com.infendro.cli.util.Regex.COMMAND
|
||||||
import com.infendro.cli.util.Regex.OPTION
|
import com.infendro.cli.util.Regex.OPTION
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
class Command private constructor(
|
class Command private constructor(
|
||||||
val name: String?,
|
val name: String?,
|
||||||
@@ -23,6 +25,7 @@ class Command private constructor(
|
|||||||
println(e.message)
|
println(e.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Dsl
|
||||||
class Builder internal constructor(
|
class Builder internal constructor(
|
||||||
private val level: Int,
|
private val level: Int,
|
||||||
private val name: String?,
|
private val name: String?,
|
||||||
@@ -68,10 +71,10 @@ class Command private constructor(
|
|||||||
arguments += argument
|
arguments += argument
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Argument<*>> T.register() = also(::argument)
|
|
||||||
|
|
||||||
fun arguments(vararg arguments: Argument<*>) = arguments.forEach(::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<*>) {
|
fun option(option: Option<*>) {
|
||||||
// validate
|
// validate
|
||||||
@@ -85,10 +88,10 @@ class Command private constructor(
|
|||||||
options += option
|
options += option
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Option<*>> T.register() = also(::option)
|
|
||||||
|
|
||||||
fun options(vararg options: Option<*>) = options.forEach(::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) {
|
fun execute(block: Context.() -> Unit) {
|
||||||
if (::_execute.isInitialized)
|
if (::_execute.isInitialized)
|
||||||
|
|||||||
@@ -84,28 +84,19 @@ sealed class Option<T : Any>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun string(vararg names: String) =
|
fun string(vararg names: String) = option(StringParser, *names)
|
||||||
Required(StringParser, names.asList())
|
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) =
|
inline fun <reified T : Enum<T>> enum(vararg names: String) = option(enumParser<T>(), *names)
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
package com.infendro.cli.command.context
|
||||||
|
|
||||||
|
import com.infendro.cli.Dsl
|
||||||
import com.infendro.cli.command.Argument
|
import com.infendro.cli.command.Argument
|
||||||
import com.infendro.cli.command.Command
|
import com.infendro.cli.command.Command
|
||||||
import com.infendro.cli.command.Option
|
import com.infendro.cli.command.Option
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
@Dsl
|
||||||
class Context internal constructor(
|
class Context internal constructor(
|
||||||
val command: Command,
|
val command: Command,
|
||||||
val commands: List<Cmd>,
|
val commands: List<Cmd>,
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.cli.exception.run
|
||||||
|
|
||||||
|
class ExecuteException(
|
||||||
|
override val message: String,
|
||||||
|
) : CliException()
|
||||||
@@ -3,9 +3,6 @@ package com.infendro.cli.parser
|
|||||||
import com.infendro.cli.exception.run.ParseException
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
object DoubleParser : Parser<Double> {
|
object DoubleParser : Parser<Double> {
|
||||||
override fun parse(text: String): Double = try {
|
override fun parse(text: String): Double = text.toDoubleOrNull()
|
||||||
text.toDouble()
|
?: throw ParseException(text, "Double")
|
||||||
} catch (_: Exception) {
|
|
||||||
throw ParseException(text, "Double")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,8 @@ class EnumParser<T : Enum<T>>(
|
|||||||
private val name: String,
|
private val name: String,
|
||||||
private val values: List<T>,
|
private val values: List<T>,
|
||||||
) : Parser<T> {
|
) : Parser<T> {
|
||||||
override fun parse(text: String): T = try {
|
override fun parse(text: String): T = values.firstOrNull { it.name == text }
|
||||||
values.first { it.name == text }
|
?: throw ParseException(text, name)
|
||||||
} catch (_: Exception) {
|
|
||||||
throw ParseException(text, name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T : Enum<T>> enumParser() = EnumParser(T::class.simpleName!!, enumEntries<T>())
|
inline fun <reified T : Enum<T>> enumParser() = EnumParser(T::class.simpleName!!, enumEntries<T>())
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package com.infendro.cli.parser
|
|||||||
import com.infendro.cli.exception.run.ParseException
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
object FloatParser : Parser<Float> {
|
object FloatParser : Parser<Float> {
|
||||||
override fun parse(text: String): Float = try {
|
override fun parse(text: String): Float = text.toFloatOrNull()
|
||||||
text.toFloat()
|
?: throw ParseException(text, "Float")
|
||||||
} catch (_: Exception) {
|
|
||||||
throw ParseException(text, "Float")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package com.infendro.cli.parser
|
|||||||
import com.infendro.cli.exception.run.ParseException
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
object IntParser : Parser<Int> {
|
object IntParser : Parser<Int> {
|
||||||
override fun parse(text: String): Int = try {
|
override fun parse(text: String): Int = text.toIntOrNull()
|
||||||
text.toInt()
|
?: throw ParseException(text, "Int")
|
||||||
} catch (_: Exception) {
|
|
||||||
throw ParseException(text, "Int")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package com.infendro.cli.parser
|
|||||||
import com.infendro.cli.exception.run.ParseException
|
import com.infendro.cli.exception.run.ParseException
|
||||||
|
|
||||||
object LongParser : Parser<Long> {
|
object LongParser : Parser<Long> {
|
||||||
override fun parse(text: String): Long = try {
|
override fun parse(text: String): Long = text.toLongOrNull()
|
||||||
text.toLong()
|
?: throw ParseException(text, "Long")
|
||||||
} catch (_: Exception) {
|
|
||||||
throw ParseException(text, "Long")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.infendro.cli.parser
|
|
||||||
|
|
||||||
object RegexParser : Parser<Regex> {
|
|
||||||
override fun parse(text: String) = Regex(text)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user