RichTea module which wraps the commons-cli Java library, providing command-line argument parsing directly from within a RichTea program.
| Function | Description |
|---|---|
CommandLine |
Parses command-line arguments according to defined Options. Returns an object whose properties map to parsed option values. |
Option |
Declares a single command-line option (used inside the CommandLine options branch). |
Parses args (defaults to the program's own arguments via env.mainArgs) using the options defined in the implicit : branch.
On success, the returned object has a property for each matched option name. Each property is a commons-cli Option instance — access .value to get the parsed string value.
On parse failure, the :parseError branch is executed and usage help is printed automatically.
| Attribute | Default | Description |
|---|---|---|
args |
env.mainArgs |
The argument array to parse. Defaults to the JVM arguments passed to the program. |
| Branch | Description |
|---|---|
: (implicit / options) |
Contains Option(...) declarations. |
:parseError |
Executed when argument parsing fails (e.g. a required option is missing). |
Declares a single command-line option. Must appear inside a CommandLine :options branch.
| Attribute | Default | Description |
|---|---|---|
opt (implicit) |
— | The short option name (e.g. "in" → -in). |
desc |
— | Human-readable description shown in help output. |
longOpt |
— | Long option name (e.g. "input" → --input). |
argName |
— | Name of the argument value shown in help output. |
numberOfArgs |
0 |
Number of argument values the option accepts. Use 1 for a single value. |
required |
true |
Whether the option must be provided. |
Import("*" from:"modules/std")
Import("*" from:"modules/cli")
Let(cli:@CommandLine({
Option("in" desc:"Input file" numberOfArgs:1 required:true)
Option("out" desc:"Output file" numberOfArgs:1 required:true)
Option("debug" desc:"Enable debug mode" required:false)
} :parseError{
Print("Invalid arguments — exiting.")
SystemExit(1)
}))
Print("Input: { cli.in.value }")
Print("Output: { cli.out.value }")
Run via:
./gradlew run -Pargs="-in input.txt -out output.txt"Import("*" from:"modules/std")
Import("*" from:"modules/cli")
Let(cli:@CommandLine({
Option("verbose" desc:"Verbose output" required:false)
}))
// cli.verbose will be null if not supplied
If(cli.verbose != null :{
Print("Verbose mode enabled.")
} :else{
Print("Running quietly.")
})
Import("*" from:"modules/std")
Import("*" from:"modules/cli")
Let(cli:@CommandLine({
Option("f" longOpt:"file" desc:"File to process" numberOfArgs:1 argName:"FILE" required:true)
}))
Print("Processing: { cli.f.value }")
Run via:
./gradlew run -Pargs="--file mydata.csv"