Skip to content

Instantly share code, notes, and snippets.

@rodrigogs
Last active October 22, 2021 19:09
Show Gist options
  • Save rodrigogs/e6ad735fcc7b78904cb339a4539fd678 to your computer and use it in GitHub Desktop.
Save rodrigogs/e6ad735fcc7b78904cb339a4539fd678 to your computer and use it in GitHub Desktop.
Kotlin simple terminal CLI
import java.util.*
inline fun <reified T> prompt(text: String): T {
return prompt(text, null)
}
inline fun <reified T> prompt(text: String, default: T?): T {
return when (T::class) {
String::class -> promptString(text, default as String) as T
Int::class -> promptInt(text, default as Int) as T
Boolean::class -> promptBoolean(text, default as Boolean) as T
else -> throw Exception("Invalid type")
}
}
fun promptString(text: String, default: String?): String {
val question = if (default == null) text else "$text ($default)"
print(question)
val result = readLine()
if (default == null && (result == null || result.isNotEmpty())) {
println("Please type your answer")
return promptString(text, default)
}
return result ?: default!!
}
fun promptInt(text: String, default: Int?): Int {
val question = if (default == null) text else "$text ($default)"
print(question)
val result = readLine()
if (default == null && (result == null || result.toIntOrNull() == null)) {
println("Please type a valid integer number")
return promptInt(text, default)
}
return result?.toInt() ?: default!!
}
fun parseBoolean(text: String?): Boolean? {
return if (text == null)
null
else
when (text.lowercase(Locale.getDefault())) {
"y" -> true
"true" -> true
"n" -> false
"false" -> false
else -> null
}
}
fun booleanToString(bool: Boolean?): String? {
return if (bool == null)
null
else
when (bool) {
true -> "Y"
false -> "N"
}
}
fun promptBoolean(text: String, default: Boolean?): Boolean {
val stringDefaultValue = booleanToString(default)
val question = if (stringDefaultValue == null) text else "$text ($stringDefaultValue)"
print(question)
val result = readLine()
val parsed = parseBoolean(result)
if (default == null && parsed == null) {
println("Please type a valid option (Y/N)")
return promptBoolean(text, default)
}
return (parsed ?: default) as Boolean
}
object Cli {
val eraseDatabase: Boolean by lazy {
prompt("Erase database?", false)
}
val fillDatabase: Boolean by lazy {
prompt("Fill database?", false)
}
val eventStream: Boolean by lazy {
prompt("Enable event stream?", false)
}
}
@rodrigogs
Copy link
Author

You can use it like:

if (Cli.fillDatabase) doSomething()

@rodrigogs
Copy link
Author

To create your custom param, just add a new value to the Cli object:

object Cli {
   ...
   
   val myProp: String by lazy {
     prompt("What is your name?", "Rodrigo")
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment