Compare commits

..

2 Commits

Author SHA1 Message Date
6be1957d82 update README 2025-12-13 20:24:01 +01:00
757011af85 small refactor 2025-12-13 20:23:34 +01:00
4 changed files with 27 additions and 47 deletions

View File

@@ -1,16 +1,15 @@
# Kotlin CLI # cli-kt
This library implements a lightweight framework for implementing terminal applications. `cli-kt` is a Kotlin Multiplatform framework for implementing terminal applications.
## Features ## Targets
* Declare commands. | Target | Status |
* Supports both named and wildcard commands. |------------------|---------------|
* Declare and parse type-safe arguments. | JVM | Supported |
* Multiplatform support | JavaScript | Supported |
* JVM | Native (Linux) | Supported |
* JavaScript | Native (Windows) | Not Supported |
* Native (Linux)
## Installation ## Installation

View File

@@ -1,6 +1,5 @@
package com.infendro.cli.command package com.infendro.cli.command
import com.infendro.cli.command.Execution
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
@@ -24,8 +23,7 @@ class Command private constructor(
fun run( fun run(
args: Array<String>, args: Array<String>,
) { ) {
val index = args val index = args.withIndex()
.withIndex()
.find { (_, value) -> value.startsWith("--") } .find { (_, value) -> value.startsWith("--") }
?.index ?: args.size ?.index ?: args.size
@@ -89,8 +87,7 @@ class Command private constructor(
result += Execution.Arg(name, value) result += Execution.Arg(name, value)
} }
arguments arguments.forEach { argument ->
.forEach { argument ->
val count = result.count { it.name == argument.name } val count = result.count { it.name == argument.name }
if (count !in argument.min..argument.max) if (count !in argument.min..argument.max)
throw ArgumentCountException(argument.name, argument.min, argument.max) throw ArgumentCountException(argument.name, argument.min, argument.max)
@@ -164,7 +161,7 @@ class Command private constructor(
} }
} }
class Key( class Key internal constructor(
internal val index: Int, internal val index: Int,
) )
} }

View File

@@ -19,45 +19,29 @@ class Execution(
operator fun Command.Key.getValue( operator fun Command.Key.getValue(
thisRef: Any?, thisRef: Any?,
property: KProperty<*>, property: KProperty<*>,
): String { ): String = commands[index].name
return commands[index]
.name
}
operator fun <T> Argument.Single<T>.getValue( operator fun <T> Argument.Single<T>.getValue(
thisRef: Any?, thisRef: Any?,
property: KProperty<*>, property: KProperty<*>,
): T { ): T = arguments.single { it.name == name }
return arguments
.single { it.name == name }
.let { parser.parse(it.value) } .let { parser.parse(it.value) }
}
operator fun <T> Argument.SingleOrElse<T>.getValue( operator fun <T> Argument.SingleOrElse<T>.getValue(
thisRef: Any?, thisRef: Any?,
property: KProperty<*>, property: KProperty<*>,
): T { ): T = arguments.singleOrNull { it.name == name }
return arguments ?.let { parser.parse(it.value) } ?: other
.singleOrNull { it.name == name }
?.let { parser.parse(it.value) }
?: other
}
operator fun <T> Argument.SingleOrNull<T>.getValue( operator fun <T> Argument.SingleOrNull<T>.getValue(
thisRef: Any?, thisRef: Any?,
property: KProperty<*>, property: KProperty<*>,
): T? { ): T? = arguments.singleOrNull { it.name == name }
return arguments
.singleOrNull { it.name == name }
?.let { parser.parse(it.value) } ?.let { parser.parse(it.value) }
}
operator fun <T> Argument.Multiple<T>.getValue( operator fun <T> Argument.Multiple<T>.getValue(
thisRef: Any?, thisRef: Any?,
property: KProperty<*>, property: KProperty<*>,
): List<T> { ): List<T> = arguments.filter { it.name == name }
return arguments
.filter { it.name == name }
.map { parser.parse(it.value) } .map { parser.parse(it.value) }
}
} }