Package io.github.wankdanker.clyops
Class Cli
java.lang.Object
io.github.wankdanker.clyops.Cli
Declarative, one-line-per-option CLI parsing: validation, help text, a JSON schema of the
interface, config files, environment variables and shell completion. Behavior follows
spec/SPEC.md in https://github.com/wankdanker/clyops.
Cli cli = new Cli("mytool");
cli.arg("input", "Input file", "", "path");
cli.opt("PORT", "port", "p", "8080", "Server port", "Network", "port");
cli.opt("VERBOSE", "verbose", "v", "flag", "Verbose output");
Values v = cli.run(args);
System.out.println(v.getString("input") + " " + v.getInt("PORT") + " " + v.getBool("VERBOSE"));
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRegister a positional argument.argVariadic(String name, String description, String... rule) Register a final positional argument that collects all remaining tokens.Register a command (spec section 1.7) and return it, to register its options and arguments on.The command words selected by the last parse, e.g.Tab-separated completion records (spec section 9) for the program itself.completionData(List<String> words) Tab-separated completion records (spec section 9).completionScript(String shell) Shell script that enables completion for this program (spec section 9):eval "$(prog --completion bash)".static StringdescribeRule(String rule) Help text for a validation rule (spec section 5).At most one of these options may be given.Look up a resolved value by option variable or positional name; null when absent.booleanisExplicitlySet(String lng) Whether the option came from CLI, config, or environment rather than a default.booleanWhether the option came from the command line.JSON description of the CLI (spec section 8).name()The program name shown in help and completion.At least one of these options must be given.Register an option.Register a repeatable option whose values accumulate into a list.Parse without exiting.requireCommand(String command, String description, String installHint) Declare an external command the program needs.Whenlngis given, the others must be too.static StringresolvePath(String value, String base, List<String> searchDirs) Resolve a path value againstbase(spec section 6), tryingsearchDirsfor bare names.Parse like a CLI: handles --help, --help-json-schema, --completion and --bash-completion, prints errors and exits on failure.optionholds the config file path;prefixesis comma-separated.Base for command-line path values (default: the working directory).setDescription(String text) Set the help description and return this CLI.setEffects(String... effects) What running the program does: read-only, idempotent, destructive, network.The environment to read (default: the process environment).Set the final help text and return this CLI.setPathSearch(String lng, String dirs) Fallback directories (colon-separated, relative to the root) for bare relative values of a path option.Base for default path values (default: the working directory); relative to the working directory.What the program reads on stdin;contentTypeis a MIME type or a comma-separated list.What the program writes on stdout; undeclared means text.Where an option's value came from: cli, config, env, default or unset.usage()Help text (spec section 7), for the selected command.static ObjectValidatevalueagainstruleand return its typed form: Long for int* and port (BigInteger beyond a long), Double for float*, Boolean for bool, the String otherwise.values()The resolved values from the last parse.Resolved values as JSON (spec section 10), in registration order.Greedy word wrap that keeps existing line breaks (spec section 7).
-
Field Details
-
VERSION
The clyops version this library implements.- See Also:
-
-
Constructor Details
-
Cli
A Cli for the programname, shown in help and used for completion.
-
-
Method Details
-
describeRule
Help text for a validation rule (spec section 5). -
validate
Validatevalueagainstruleand return its typed form: Long for int* and port (BigInteger beyond a long), Double for float*, Boolean for bool, the String otherwise.- Throws:
ValidationError- with the spec's error text
-
resolvePath
Resolve a path value againstbase(spec section 6), tryingsearchDirsfor bare names. -
wrapText
Greedy word wrap that keeps existing line breaks (spec section 7). -
setRoot
Base for default path values (default: the working directory); relative to the working directory. -
setCwd
Base for command-line path values (default: the working directory). Resets the root to it. -
setEnv
The environment to read (default: the process environment). -
setDescription
Set the help description and return this CLI. -
setEpilog
Set the final help text and return this CLI. -
setEffects
What running the program does: read-only, idempotent, destructive, network. -
setStdin
What the program reads on stdin;contentTypeis a MIME type or a comma-separated list. -
setStdout
What the program writes on stdout; undeclared means text. -
exclusive
At most one of these options may be given. -
requires
Whenlngis given, the others must be too. -
oneOf
At least one of these options must be given. -
command
Register a command (spec section 1.7) and return it, to register its options and arguments on. -
commandPath
The command words selected by the last parse, e.g. [db, migrate]. -
setConfig
optionholds the config file path;prefixesis comma-separated. -
requireCommand
Declare an external command the program needs. -
setPathSearch
Fallback directories (colon-separated, relative to the root) for bare relative values of a path option. -
opt
public Cli opt(String var, String lng, String shrt, String def, String description, String... groupAndRule) Register an option.defis a value, "flag", "optional", or "" (required).groupAndRuleis an optional group (default "Options") and validation rule. -
optArray
public Cli optArray(String var, String lng, String shrt, String description, String... groupAndRule) Register a repeatable option whose values accumulate into a list. -
arg
Register a positional argument. An emptydefmakes it required. -
argVariadic
Register a final positional argument that collects all remaining tokens. -
parse
Parse without exiting. Values are invalues()when the status is "ok". -
run
Parse like a CLI: handles --help, --help-json-schema, --completion and --bash-completion, prints errors and exits on failure. Returns the values. -
name
The program name shown in help and completion. -
values
The resolved values from the last parse. -
get
Look up a resolved value by option variable or positional name; null when absent. -
source
Where an option's value came from: cli, config, env, default or unset. -
isSet
Whether the option came from the command line. -
isExplicitlySet
Whether the option came from CLI, config, or environment rather than a default. -
valuesJson
Resolved values as JSON (spec section 10), in registration order. -
usage
Help text (spec section 7), for the selected command. -
jsonSchema
JSON description of the CLI (spec section 8). -
completionScript
Shell script that enables completion for this program (spec section 9):eval "$(prog --completion bash)". Null for an unknown shell. -
completionData
Tab-separated completion records (spec section 9) for the program itself. -
completionData
Tab-separated completion records (spec section 9).wordsare the words typed after the program name; a program with commands follows them.
-