A lightweight, dependency-free library for building simple, declarative command-line programs in Kotlin.
Here's a CLI calculator implemented on top of cmdloop: Kalk.
First add the bintray repo:
repositories {
maven {
url "https://zachgray.bintray.com/cmdloop"
}
}
then the binary dependency can be added:
dependencies {
compile "io.zachgray.cmdloop:cmdloop:2.0.2"
}
fun main(args:Array<String>) {
commandLoop {
// optional: a custom welcome message
welcomeMessage {
"Hi! Enter a mathematical expression to be evaluated, or enter a command."
}
// define the command prefix
commandPrefix {
"/" //example: /exit, /time
}
// define a custom command to get the current time
command("time") { _, _ ->
println(" the time is ${Date().asTimeString()}")
}
// override loop control (continue by default) with break - loop will exit
command("die", loopControl = BREAK) { _, _ ->
println("I'm dead")
}
// print any args that were passed to the program on launch
command("args") { _, _ ->
args.forEachIndexed { i, arg -> println("arg[$i]=$arg") }
}
// print command params
command("printParams") { params, _ ->
println("you included params: $params")
}
// optional: the "default" case gets executed when user input is not a command. in this case, the mathematical
// expression is evaluated and results printed.
default {
{ input ->
input?.let {
val expression = it.toRPNExpression()
println(" numbers: ${expression.numbers}")
println(" operators: ${expression.operators}")
println(" solution: ${expression.evaluate()}")
}
}
}
// optional: catch error
catch {
{ _ ->
println(" invalid expression, please try again.")
}
}
}
}
Hi! Enter a message and it will be echoed, or enter a command. Commands:
/args
/die
/exit
/getTime
/history
> /getTime
the time is 02:39
> /history
0 /getTime
1 /history
> hello
you said "hello"
> /args
arg[0]=argh
> /exit
bye! 👋
Out of the box, the following commands are included:
exit
- exit the programhistory
- view command history
To provide a custom exit function, you could do the following:
command("exit", loopControl = BREAK) {
println("custom exit!")
}
./gradlew assemble