68 lines
1.6 KiB
Markdown
68 lines
1.6 KiB
Markdown
# cli-kt
|
|
|
|
`cli-kt` is a Kotlin Multiplatform framework for implementing terminal applications.
|
|
|
|
## Targets
|
|
|
|
| Target | Status |
|
|
|------------------|---------------|
|
|
| JVM | Supported |
|
|
| JavaScript | Supported |
|
|
| Native (Linux) | Supported |
|
|
| Native (Windows) | Not Supported |
|
|
|
|
## Installation
|
|
|
|
Add the following to your `build.gradle.kts`.
|
|
|
|
```kotlin
|
|
repositories {
|
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
|
}
|
|
|
|
dependencies {
|
|
implementation("com.infendro:cli:1.4.0")
|
|
}
|
|
```
|
|
|
|
## Usage
|
|
|
|
```kotlin
|
|
fun main(args: Array<String>) = cli {
|
|
execute {
|
|
// define execution of command (e.g., display help)
|
|
}
|
|
|
|
// declare command "greet"
|
|
command("greet") {
|
|
// declare arguments
|
|
val speakersArg = Argument.string().variable().register()
|
|
|
|
// declare options
|
|
val greetingOpt = Option.string("greeting", "greet", "g").orElse("Hello").register()
|
|
val nameOpt = Option.string("name", "n").orNull().register()
|
|
|
|
execute {
|
|
// retrieve the arguments and options
|
|
val speakers by speakersArg
|
|
val greeting by greetingOpt
|
|
val name by nameOpt
|
|
|
|
println("${speakers.joinToString()}: $greeting ${name ?: "World"}!")
|
|
}
|
|
|
|
// declare more commands...
|
|
}
|
|
|
|
// declare fallback command
|
|
fallback { valueArg ->
|
|
execute {
|
|
// retrieve the value used for the fallback command
|
|
val value by valueArg
|
|
|
|
println("""command "$value" executed!""")
|
|
}
|
|
}
|
|
}.run(args)
|
|
```
|