cli module

Declare your CLI shape (flags, options, positionals), then parse argv into a map. Auto-generates a --help-style usage string.

import "cli" as cli;

var p = cli.parser("greet");
p.description = "Print a greeting.";
cli.flag      (p, {name: "shout",  short: "s", help: "Use upper case"});
cli.option    (p, {name: "lang",   short: "l", help: "en or bn", default: "en"});
cli.positional(p, {name: "name",                help: "Who to greet"});

var args = cli.parse(p);
print(args.lang, args.name, args.shout);

API

FunctionDescription
cli.parser(name) → parserStart a new parser. Set parser.description for the help blurb.
cli.flag(p, spec)Add a boolean flag. spec: {name, short?, help?}.
cli.option(p, spec)Add an option that takes a value. spec: {name, short?, help?, default?}.
cli.positional(p, spec)Add a positional arg. spec: {name, help?, default?}.
cli.parse(p) → mapRead real sys.arg(*) and produce the args map.
cli.parse_argv(p, argv) → mapSame, but parses a synthetic list (useful in tests).
cli.usage(p) → stringBuild a --help-style usage string.

Output shape

The returned map has one entry per declared name:

  • Flags: bool (default false).
  • Options: string (default per spec).
  • Positionals: string (default per spec).
  • _rest — extra positionals past the declared ones.

See also

  • sys — the low-level sys.argc() / sys.arg(i) accessors cli sits on top of.