Skip to content

RichTeaLang/lib-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

lib-cli

RichTea module which wraps the commons-cli Java library, providing command-line argument parsing directly from within a RichTea program.

Exports

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).

CommandLine

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.

Attributes

Attribute Default Description
args env.mainArgs The argument array to parse. Defaults to the JVM arguments passed to the program.

Branches

Branch Description
: (implicit / options) Contains Option(...) declarations.
:parseError Executed when argument parsing fails (e.g. a required option is missing).

Option

Declares a single command-line option. Must appear inside a CommandLine :options branch.

Attributes

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.

Examples

Basic usage

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"

Optional flag with default fallback

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.")
})

Long options

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"

About

RichTea wrapper for commons-cli (command line argument processing library)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors