Skip to content

Defining a CLI

Declare parameters

1
2
3
4
5
@Parameters(index = "0", description = ["Name to greet"])
lateinit var name: String

@Parameters(index = "1", arity = "0..*", description = ["Additional files"])
lateinit var additional: List<Path>

Run like this: ./my-script a b c and name will be a, additional will be [b, c].

Declare named options

1
2
3
4
5
6
@Option(names = ["--config", "-c"], description = ["Config file."])
lateinit var someFile: Path

// Primitive types (booleans, numbers) shouldn't use 'lateinit'.
@Option(names = ["--toggle"])
var toggle: Boolean = false

All arguments

1
echo("All arguments are:\n" + arguments.bulletedList())

Unmatched arguments

1
echo("All unmatched arguments:\n" + unmatchedArguments.bulletedList())

More

See PicoCLI.