Skip to content

Type-safe builder (DSL) support for defining commands #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,46 @@ Args: [-r, 16, CAFE, BABE, DEAD, BEEF, --sum]
Integers: [51966, 47806, 57005, 48879]
Sum: 205656
```

## Type-Safe Builders (DSL) syntax

kotlinx.cli also provides type-safe builders to make defining commands easy.
Useful if you have multiple commands you want to implement.

```kotlin
package kotlinx.cli.examples

import kotlinx.cli.*
import kotlin.system.exitProcess

fun main(args: Array<String>) {

val cli = command {
commandName = "Example2"

// Define commandName-line interface
val integers by positionalArgumentsList("N+", "Integers", minArgs = 1)
val radix by flagValueArgument("-r", "radix", "Input numbers radix", 10) { it.toInt() }
val sum by flagArgument("--sum", "Print sum")
val max by flagArgument("--max", "Print max")
val min by flagArgument("--min", "Print min")

// main block is where you do something useful
main {
if (!sum || !max || !min) {
// CommandLineContext::exitProcess() looks like standard kotlin
exitProcess(1)
}

val ints = integers.map { it.toInt(radix) }
println("Args: ${args.asList()}")
println("Integers: $ints")
if (sum) println("Sum: ${ints.sum()}")
if (max) println("Max: ${ints.max()}")
if (min) println("Min: ${ints.min()}")
}
}

val exitCode = cli.run(args)
exitProcess(exitCode)
}
35 changes: 35 additions & 0 deletions kotlinx.cli.jvm/src/main/kotlin/kotlinx/cli/examples/Example2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kotlinx.cli.examples

import kotlinx.cli.*
import kotlin.system.exitProcess

fun main(args: Array<String>) {
val cli = command {
commandName = "Example2"

// Define commandName-line interface
val integers by positionalArgumentsList("N+", "Integers", minArgs = 1)
val radix by flagValueArgument("-r", "radix", "Input numbers radix", 10) { it.toInt() }
val sum by flagArgument("--sum", "Print sum")
val max by flagArgument("--max", "Print max")
val min by flagArgument("--min", "Print min")

// main block is where you do something useful
main {
// Do something useful
if (!sum || !max || !min) {
exitProcess(1) // CommandLineContext::exitProcess() looks like standard kotlin
}

val ints = integers.map { it.toInt(radix) }
println("Args: ${args.asList()}")
println("Integers: $ints")
if (sum) println("Sum: ${ints.sum()}")
if (max) println("Max: ${ints.max()}")
if (min) println("Min: ${ints.min()}")
}
}

val exitCode = cli.run(args)
exitProcess(exitCode)
}
27 changes: 27 additions & 0 deletions src/main/kotlin/kotlinx/cli/CommandLineApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kotlinx.cli

class CommandLineApplication: CommandLineInterface("") {
private lateinit var exec: CommandLineContext.(args: Array<String>) -> Unit

/**
* The main fun signature is intended to look like standard main fun so that it is easy to adopt
*/
infix fun main(func: CommandLineContext.(args: Array<String>) -> Unit) {
exec = func
}

fun run(args: Array<String>): Int {
return try {
// parse the command line arguments
parse(args)
CommandLineContext().apply { exec(args) }.statusCode
} catch (exitProcess: CommandLineError) {
exitProcess.status
} catch (throwable: Throwable) {
1
}
}
}

fun command(func: CommandLineApplication.() -> Unit): CommandLineApplication =
CommandLineApplication().apply(func)
13 changes: 13 additions & 0 deletions src/main/kotlin/kotlinx/cli/CommandLineContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kotlinx.cli

/**
* This class executes the main{} block and defines exitProcess so that Process::exitProcess isn't called
* directly.
*/
class CommandLineContext {
var statusCode: Int = 0

fun exitProcess(status: Int): Int {
throw CommandLineError(status)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/kotlinx/cli/CommandLineError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kotlinx.cli

class CommandLineError(val status: Int) : Throwable()
2 changes: 1 addition & 1 deletion src/main/kotlin/kotlinx/cli/CommandLineInterface.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kotlinx.cli

open class CommandLineInterface(
val commandName: String,
var commandName: String,
private val usage: String? = null,
private val description: String? = null,
private val epilogue: String? = null,
Expand Down