# bend-cli `bend-cli` is a command-line interface library for Bend. Declare commands with usage strings and decode typed arguments; a single definition produces help text, usage errors with typo suggestions, ANSI color, shell completions, and man pages. Because parsing and rendering are pure functions, applications can state and prove their CLI behavior as Bend laws. The BendHub package is `cli`. Import `cli.bend` in application logic and laws; import `run.bend` only in the `IO` entry point. Bend 2.0.40 or newer is required. ## Quickstart Create `app.bend` with the command definition, decoder, and plan: ```python import Base import cli@0.3.0.0/cli.bend as Cli type Greeting is Data: Greeting{name: String} def cli() -> Cli.Cmd: Cli.cmd("hello", "Print a greeting", [ Cli.arg("[NAME]", "Who to greet", [Cli.default("world")]), ]) def decode(+m: Cli.Matches) -> Cli.Dec(Greeting): do Cli.Dec: name : String <- Cli.str(m, "name") return Greeting{name} def plan(+t: Cli.Term, greeting: Greeting) -> Cli.Ran: match greeting: case Greeting{name}: Cli.Ran{0, ("Hello, " ++ name ++ "!\n" : String), ""} ``` `plan` returns what the command prints and how it exits as `Cli.Ran{code, out, err}`: its exit status and its stdout and stderr text. It does no IO, so laws can state what the binary prints (see [State and prove CLI behavior](#state-and-prove-cli-behavior)). Create `main.bend` to wire the command into process arguments, terminal probing, and standard streams: ```python import Base import ./app.bend as App import cli@0.3.0.0/run.bend as Run def main() -> IO(Unit): Run.planned(~App.Greeting, ~App.decode, ~App.plan, App.cli()) ``` Build and run the binary: ```sh bend main.bend -o hello ./hello Ada # Hello, Ada! ./hello --help # full help ``` `-h` prints compact help (the first paragraph of each description); `--help` prints every paragraph. Usage errors write to stderr and exit with status `2`, or the root's `Cli.usage_status`. Invalid command definitions and decoder bugs write a `bug:` diagnostic to stderr and exit with status `70`. See [`examples/hello`](examples/hello) for this quickstart with its laws and proofs, [`examples/grep`](examples/grep) for a multi-option command with option headings, exclusive output modes, a negatable flag, environment fallbacks, colored output, and file I/O, and [`examples/argbench`](examples/argbench) for the benchmark CLI behind [Performance](#performance). ## Define commands `Cli.cmd(name, about, items)` defines a root command. Add `Cli.sub` for subcommands and `Cli.arg` for options or positionals. An argument's usage string determines its kind, flags, placeholder, and default ID: | Usage string | Meaning | |---|---| | `-v, --verbose` | Boolean flag | | `-v, --verbose...` | Occurrence count | | `--[no-]cache` | Negatable flag | | `-o, --output ` | Option with one value | | `--include ...` | Repeatable option | | `--color[=]` | Option with an optional value; requires `Cli.bare(...)` | | `` / `[OUTPUT]` | Required / optional positional | | `...` / `[FILE]...` | One or more / zero or more positionals | An argument's ID defaults to its long option name (`"output"`), its short flag character when no long name is declared (`"v"`), or its lowercased placeholder name for positionals (`"input"`). A negatable `--[no-]cache` has the ID `"cache"`, while a plain `--no-clobber` keeps `"no-clobber"`. Use `Cli.id(...)` to override the derived ID. Use argument attributes to declare types, value sources, and constraints. For example, `Cli.as_u32()` validates values during parsing, `Cli.env("PORT")` reads an environment fallback, `Cli.default("8080")` supplies a default, and `Cli.one_of(["text", "json"])` restricts accepted values. Values resolve in precedence order: `argv`, the first set and non-empty variable from one or more `Cli.env` attributes, and finally `Cli.default`. Relationship constraints (`Cli.conflicts`, `Cli.requires`, `Cli.exclusive`, and `Cli.need_one`) apply to values supplied by `argv` or the environment, not to defaults. `Cli.delim(',')` on a repeatable option or a variadic positional splits each of its values at the separator, before any type check, and keeps empty pieces (`a,,b` gives `a`, an empty value, and `b`). It splits every value the argument takes, from `argv`, the environment, or a default, including operands after `--`. Once a `Cli.rest()` positional has its first value, every later token is one of its values, `--` and tokens that start with `-` included, and is split too. With `-v` and `[FILES]...` declared with `Cli.delim(',')`: ```text $ tool x,y -v -- -v,a # files: x, y, -v, a (the first -v is the flag) $ tool a,b -v,x -- c # with Cli.rest() too: a, b, -v, x, --, c ``` Options may appear between positional operands. Short flags cluster (`-abc`), options accept attached values (`-ofile`, `--output=file`), and a repeated single-value option retains its last occurrence. Option and subcommand names match exactly; close misspellings receive suggestions. `Cli.hyphen()` lets a value start with `-`. An option with it takes any next token but `--` as its value, even `-h`. A positional with it takes a token that starts with `-`, short or long, when the token names no option in scope: `-x` and `--x=1` are values, while a declared `-v` or `--verbose=1` and the built-in help and version options stay options. `Cli.rest()` on the last, variadic positional does the same for its first value, and every later token is one of its values. With `-v, --verbose`, ``, and `[ARGS]...` declared with `Cli.rest()`: ```text $ retry curl -fsS --max-time=5 url # args: -fsS, --max-time=5, url $ retry curl -v url # -v is retry's flag; args: url $ retry curl -- -v url # args: -v, url ``` For a program and its arguments in one slot, as `timeout`, `nice`, and `env` take them, declare `...` with `Cli.rest()` and read it with `Cli.program(m, "command")`. It returns a `Cli.Program{name, args}`: the slot's first value, then every later token in order. So `retry curl --help` and `retry curl -v url` give curl `--help` and `-v url`, where the two slots above print retry's help and take `-v` as retry's flag. Read `[COMMAND]...` with `Cli.program_opt`, which gives `None{}` when the slot is empty. `Cli.check` reports `Cli.program` on a slot without `Cli.rest()`, which would still take the command's own options from among the program's arguments. Keep two slots when the command's options may follow the name, as in the pkg demo's `run