Compare commits
2 Commits
ebd698e611
...
6be1957d82
| Author | SHA1 | Date | |
|---|---|---|---|
|
6be1957d82
|
|||
|
757011af85
|
19
README.md
19
README.md
@@ -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.
|
||||
* Supports both named and wildcard commands.
|
||||
* Declare and parse type-safe arguments.
|
||||
* Multiplatform support
|
||||
* JVM
|
||||
* JavaScript
|
||||
* Native (Linux)
|
||||
| Target | Status |
|
||||
|------------------|---------------|
|
||||
| JVM | Supported |
|
||||
| JavaScript | Supported |
|
||||
| Native (Linux) | Supported |
|
||||
| Native (Windows) | Not Supported |
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.infendro.cli.command
|
||||
|
||||
import com.infendro.cli.command.Execution
|
||||
import com.infendro.cli.argument.Argument
|
||||
import com.infendro.cli.exception.build.*
|
||||
import com.infendro.cli.exception.run.ArgumentCountException
|
||||
@@ -24,8 +23,7 @@ class Command private constructor(
|
||||
fun run(
|
||||
args: Array<String>,
|
||||
) {
|
||||
val index = args
|
||||
.withIndex()
|
||||
val index = args.withIndex()
|
||||
.find { (_, value) -> value.startsWith("--") }
|
||||
?.index ?: args.size
|
||||
|
||||
@@ -89,8 +87,7 @@ class Command private constructor(
|
||||
result += Execution.Arg(name, value)
|
||||
}
|
||||
|
||||
arguments
|
||||
.forEach { argument ->
|
||||
arguments.forEach { argument ->
|
||||
val count = result.count { it.name == argument.name }
|
||||
if (count !in 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,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -19,45 +19,29 @@ class Execution(
|
||||
operator fun Command.Key.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): String {
|
||||
return commands[index]
|
||||
.name
|
||||
}
|
||||
): String = commands[index].name
|
||||
|
||||
operator fun <T> Argument.Single<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T {
|
||||
return arguments
|
||||
.single { it.name == name }
|
||||
): T = arguments.single { it.name == name }
|
||||
.let { parser.parse(it.value) }
|
||||
}
|
||||
|
||||
operator fun <T> Argument.SingleOrElse<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T {
|
||||
return arguments
|
||||
.singleOrNull { it.name == name }
|
||||
?.let { parser.parse(it.value) }
|
||||
?: other
|
||||
}
|
||||
): T = arguments.singleOrNull { it.name == name }
|
||||
?.let { parser.parse(it.value) } ?: other
|
||||
|
||||
operator fun <T> Argument.SingleOrNull<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): T? {
|
||||
return arguments
|
||||
.singleOrNull { it.name == name }
|
||||
): T? = arguments.singleOrNull { it.name == name }
|
||||
?.let { parser.parse(it.value) }
|
||||
}
|
||||
|
||||
operator fun <T> Argument.Multiple<T>.getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
): List<T> {
|
||||
return arguments
|
||||
.filter { it.name == name }
|
||||
): List<T> = arguments.filter { it.name == name }
|
||||
.map { parser.parse(it.value) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user