std/args
import "std/args"
Cli cli = new_cli("greet", "Greets someone")
set_version(cli, "1.0")
add_flag(cli, "loud", "l", "Shout the greeting")
add_option(cli, "name", "n", "NAME", "Who to greet", "world")
add_color_option(cli)
Args a = parse_args(cli, args())
str greeting = "Hello, " + option(a, "name")
if flag(a, "loud") then
greeting = upper(greeting)
end
print(greeting)greet --help then prints:
greet 1.0 - Greets someone
Usage: greet [options]
Options:
-l, --loud Shout the greeting
-n, --name <NAME> Who to greet (default: world)
--color <WHEN> Use colors: auto, always, or never (default: auto)
-h, --help Show this help
-V, --version Show the version| Function | Does |
|---|---|
new_cli(name, about) |
starts a description of the program's arguments |
set_version(cli, version) |
adds -V and --version |
add_flag(cli, name, short, help) |
a flag such as --loud or -l; short may be "" |
add_option(cli, name, short, value_name, help, fallback) |
an option with a value, or fallback when it is not given |
add_command(cli, name, help) |
a subcommand: the first positional argument must be one |
add_positional(cli, name, help) |
describes a positional argument in the help |
set_rest_count(cli, min, max) |
limits the positional arguments; max -1 for any number |
add_color_option(cli) |
adds --color auto/always/never, applied by parse_args |
parse_args(cli, argv) |
parses args(); prints help, the version, or an error and exits when needed |
try_parse_args(cli, argv) |
the same as a Result, without printing or exiting |
flag(a, name) |
whether a flag was given |
option(a, name) |
an option's value, or its fallback |
option_int(a, name) |
the value as a Result[int] |
given(a, name) |
whether an option was given at all |
help_text(cli) |
the help as --help prints it |
apply_color(a) |
applies --color from add_color_option, as parse_args does after try_parse_args |
edit_distance(a, b) |
how many one-character edits turn a into b |
did_you_mean(word, choices, prefix) |
" (did you mean '--name'?)" for the closest choice, with prefix before it, or "" when none is close |
Args holds command, the subcommand or "", and rest, the positional
arguments. Options can be written --name value, --name=value,
-n value, or -nvalue, short flags can be grouped as -lv, and -- ends
the options. Errors come with a suggestion when one is close, such as
unknown option '--verbos' (did you mean '--verbose'?), and end the program
with status 2.