std.cli
Command-line options read into a record, and commands into a union, with derive CommandLine.
strata run and a compiled program build main’s options from the arguments and answer --help from the doc comments.
OptSpec
Section titled “OptSpec”type OptSpec = { name: String, flag: String, /// Its short name (`m` for `-m`), or `""`. short: String, kind: OptKind, placeholder: String, /// Its doc comment on one line, without its `-m` or `[name]`. doc: String, /// What `--help` says of it. text: String, choice: Bool, /// For a command field, each command's name and doc. commands: List[(String, String)], /// For a flag, whether it is on unless `--no-` and its name is given. on: Bool = false,}One field, as the parser and --help see it.
OptKind
Section titled “OptKind”type OptKind = | FlagOpt | OptionalOpt | DefaultedOpt | RepeatedOpt | RestOpt | PositionalOpt | MaybeOpt | CommandOptHow a field is given on the command line.
type Given = { values: List[(String, List[String])] }The texts the arguments gave each field, in order. A field given several times has an entry each time.
CliError
Section titled “CliError”type CliError = | ShowHelp { text: String } | BadArgs { message: String }The help asked for with --help, or what is wrong with the arguments.
Traits
Section titled “Traits”CommandLine
Section titled “CommandLine”derivable(Record | Union) trait CommandLine { /// The options from the program's arguments, every refinement checked. `Err` holds the help when asked for, or what is wrong. derived fn from_args( program: String, args: List[String], ) -> Result[Self, CliError]}A record whose fields are a program’s command-line options, or a union of its commands. Each field of a record becomes:
Bool: a flag,--lines, false unless given.--no-linesmakes it
false.
Option[T]: an option,--limit 3or--limit=3,Noneunless given.
When its doc starts with its name in brackets (/// [module] The module.), an optional positional argument instead, filled after the
required ones from the arguments before a --.
List[T]: the remaining positional arguments, in order. Only one field
can be this. When its doc starts with its flag (/// --rm A definition.),
an option that may be repeated: --rm a --rm b.
- a union that derives
CommandLine: a command, named by the first
positional argument, which takes all the arguments after it. The record’s other fields are options given before it.
- anything else: a required positional argument in field order, or an
option (--times 3) when the field has a default.
Other rules:
- A field left out takes its default. A flag’s default can be
true:
--help then shows it as --no-lines. A list’s default is used when no
arguments are left.
Tis anInt, aFloat, aString, a refinement of one, or a
Choice: type Level = Quiet | Loud is quiet or loud.
- Names are written with dashes:
dry_runis--dry-run. - An option or flag whose doc starts with
-and a letter (`/// -n Stop
after…) also has that short name: -n 3`.
- In a union of commands, each member is a record or an atom, named in
kebab case (DryRun is dry-run). --help lists the commands, and tool edit --help a command’s own options.
Functions
Section titled “Functions”fn kebab(name: String) -> StringDryRun -> dry-run
fn parse( program: String, specs: List[OptSpec], args: List[String], check: (String, String) -> Result[(), String], help: String,) -> Result[Given, CliError] terminatesRead the arguments with the options’ descriptions; check checks one
field’s text as the parser meets it.
help_text
Section titled “help_text”fn help_text( program: String, doc: List[String], specs: List[OptSpec],) -> StringWhat --help prints.
read_input
Section titled “read_input”fn read_input[C: Console, F: Files]( mut console: C, files: F, path: String,) -> Result[String, FsError]A file’s text, or standard input’s when path is -.
commands_help
Section titled “commands_help”fn commands_help( program: String, doc: List[String], commands: List[(String, String)],) -> StringWhat --help prints for a union of commands.
styled_help
Section titled “styled_help”fn styled_help(text: String, p: Paint) -> StringWhat --help prints, with its section headings (Usage:, Options:, …)
in bold when the paint is on.
error_line
Section titled “error_line”fn error_line(message: String, p: Paint) -> Stringerror: message, with error: in bold red when the paint is on.