Skip to content

Arguments

abstract

Base class for declaring a command line with by delegates.

Declare each parameter with option, flag, or argument. Reading a delegated property before the arguments have been parsed throws.

class ServerArgs : Arguments(programName = "serve", description = "Run the server") {
    val host: String        by option("--host", help = "Bind host").default("0.0.0.0")
    val port: Int           by option("--port", "-p", help = "Port").int().required()
    val verbose: Boolean    by flag("--verbose", "-v", help = "Verbose logging")
    val files: List<String> by argument(help = "Files to serve").multiple()
}

fun main(argv: Array<String>) {
    val args = ServerArgs().parsed(argv)
    println("serving ${args.files} on ${args.host}:${args.port}")
}
Arguments.kt

Constructors

Arguments

Arguments(programName: String, description: String, version: String?)
programName

the program name shown in usage and help.

description

a one-line description shown at the top of --help.

version

when set, --version is recognized.

Arguments.kt

Properties

description

val description: String
Arguments.kt

programName

val programName: String
Arguments.kt

version

val version: String?
Arguments.kt

Released under the Apache License 2.0.