1.4.0 release #9

Merged
Infendro merged 20 commits from develop into main 2026-01-10 07:37:21 +00:00
11 changed files with 55 additions and 75 deletions
Showing only changes of commit 0a8e51c7ab - Show all commits

View File

@@ -0,0 +1,4 @@
package com.infendro.cli
@DslMarker
internal annotation class Dsl()

View File

@@ -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)

View File

@@ -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)

View File

@@ -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())

View File

@@ -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>,

View File

@@ -0,0 +1,5 @@
package com.infendro.cli.exception.run
class ExecuteException(
override val message: String,
) : CliException()

View File

@@ -3,9 +3,6 @@ package com.infendro.cli.parser
import com.infendro.cli.exception.run.ParseException
object DoubleParser : Parser<Double> {
override fun parse(text: String): Double = try {
text.toDouble()
} catch (_: Exception) {
throw ParseException(text, "Double")
}
override fun parse(text: String): Double = text.toDoubleOrNull()
?: throw ParseException(text, "Double")
}

View File

@@ -7,11 +7,8 @@ class EnumParser<T : Enum<T>>(
private val name: String,
private val values: List<T>,
) : Parser<T> {
override fun parse(text: String): T = try {
values.first { it.name == text }
} catch (_: Exception) {
throw ParseException(text, name)
}
override fun parse(text: String): T = values.firstOrNull { it.name == text }
?: throw ParseException(text, name)
}
inline fun <reified T : Enum<T>> enumParser() = EnumParser(T::class.simpleName!!, enumEntries<T>())

View File

@@ -3,9 +3,6 @@ package com.infendro.cli.parser
import com.infendro.cli.exception.run.ParseException
object FloatParser : Parser<Float> {
override fun parse(text: String): Float = try {
text.toFloat()
} catch (_: Exception) {
throw ParseException(text, "Float")
}
override fun parse(text: String): Float = text.toFloatOrNull()
?: throw ParseException(text, "Float")
}

View File

@@ -3,9 +3,6 @@ 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")
}
override fun parse(text: String): Int = text.toIntOrNull()
?: throw ParseException(text, "Int")
}

View File

@@ -3,9 +3,6 @@ package com.infendro.cli.parser
import com.infendro.cli.exception.run.ParseException
object LongParser : Parser<Long> {
override fun parse(text: String): Long = try {
text.toLong()
} catch (_: Exception) {
throw ParseException(text, "Long")
}
override fun parse(text: String): Long = text.toLongOrNull()
?: throw ParseException(text, "Long")
}