@std/cli/command.ms#
Import each required symbol with import Name from "@std/cli/command.ms".
Arg Type#
A positional argument, bound in declaration order.
type Arg {
name String
help String = ""
required Bool = true
rest Bool = false
kind FlagKind = FlagKind.String
}Arg.help#
One line shown in help.
help String = ""Arg.kind#
What the argument accepts: String (the default), Int or Float. An Int or
Float argument fills a record field of that type and fails the run when the word
does not parse. Bool and List are not positional kinds, and a rest argument
is always List[String].
kind FlagKind = FlagKind.StringArg.name#
The key Matches reads it by.
name StringArg.required#
Absence is an error. A required argument may not follow an optional one.
required Bool = trueArg.rest#
Takes every remaining word as a list; only the last argument may be rest.
rest Bool = falseCommand Type#
A command or command group: its schema, subcommands and handler.
A group is a Command with children. Build a leaf with command[T], then call execute(arguments()) from main. Start with the complete CLI recipe
; use exec directly when you need Matches.
type Command {
name String
about String = ""
flags List[Flag] = []
args List[Arg] = []
children List[Command] = []
exec fn(Matches) Result[Int, Error] = m => ok(0)
}Command.about#
One line shown in help.
about String = ""Command.args#
Positional arguments in order.
args List[Arg] = []Command.children#
Subcommands in help order; their names must be unique, nonempty and not help.
children List[Command] = []Command.exec#
Runs after a successful parse; the returned Int is the exit status.
exec fn(Matches) Result[Int, Error] = m => ok(0)Command.flags#
Flags declared here; persistent ones also apply below.
flags List[Flag] = []Command.name#
The word that selects this node; the program name for the root.
name StringCommand.child#
Returns the child named word, or null; the result reads the child in place.
fn child(word String) Command?Command.execute#
Parses argv, writes help to stdout or the error plus a usage hint to stderr,
and runs the handler. Returns the handler’s status, 0 after help, 1 when the
handler fails and 2 for a usage error.
fn execute(argv List[String]) IntCommand.help#
Returns the help text for the command reached by following path from this node,
or null for an unknown path.
fn help(path List[String] = []) String?Command.parse#
Checks the command tree, resolves the subcommand chain, and validates flags and
arguments. Returns Help for -h, --help, a leading help word, or a group
invoked without a subcommand; an invalid schema or usage error returns EINVAL.
fn parse(argv List[String]) Result[Parsed, Error]Command.run#
Runs the handler of the command matches.path names, as parse resolved it, and
returns its exit status. The path starts at this node’s name; an unknown path is
an EINVAL error.
fn run(matches Matches) Result[Int, Error]Flag Type#
A named option such as --port 8080 or -p 8080.
type Flag {
name String
kind FlagKind = FlagKind.String
short String = ""
default String? = null
help String = ""
required Bool = false
persistent Bool = false
choices List[String] = []
}Flag.choices#
When not empty, every value must be one of these texts; use with String and List flags.
choices List[String] = []Flag.default#
Text used when the flag is absent; null for no default. A Bool defaults to false.
default String? = nullFlag.help#
One line shown in help.
help String = ""Flag.kind#
What the flag accepts, and so the type of the record field; defaults to String.
kind FlagKind = FlagKind.StringFlag.name#
The long name without dashes; also the key Matches reads it by.
name StringFlag.persistent#
Also accepted by every subcommand below the command that declares it.
persistent Bool = falseFlag.required#
Absence is an error when there is no default. A Bool or List flag is never absent
(false or empty), so required has no effect on it.
required Bool = falseFlag.short#
One-letter alias used as -p; empty for none. A value follows as -p 8080 or
-p=8080; short flags do not combine (-vd) or attach a value (-p8080).
short String = ""FlagKind Enum#
What a flag accepts. Each case fills the record field of the same language type.
enum FlagKind {
Bool
String
Int
Float
List
}FlagKind.Bool#
Bool field. Present or absent; --name, --name=false and --no-name are accepted.
BoolFlagKind.Float#
Float field. A finite decimal number that parses as Float; other text, including NaN
and infinities, returns an Error with kind "EINVAL".
FloatFlagKind.Int#
Int field. A whole number that fits in Int; other text returns an Error with kind "EINVAL".
IntFlagKind.List#
List[String] field. The flag may repeat; every occurrence is kept in order.
ListFlagKind.String#
String field. Any text. The value may begin with a dash (--name=-x or --name -x).
StringMatches Type#
The values one invocation produced.
Reading a name the schema does not declare, or reading it as the wrong kind, is a programmer error and faults.
A flag with a default or required, and a required argument, always has a value.
type Matches {
values Map[String, List[String]] = Map[String, List[String]]()
path List[String] = []
fields Map[String, String] = Map[String, String]()
}Matches.fields#
The JSON literal for each supplied value by field name (dashes in names become
underscores); decoded by command.
fields Map[String, String] = Map[String, String]()Matches.path#
The command names walked from the root, root included.
path List[String] = []Matches.values#
Text values by flag or argument name; a List flag or rest argument holds several.
values Map[String, List[String]] = Map[String, List[String]]()Matches.bool#
Returns a Bool flag value; absent is false.
fn bool(name String) BoolMatches.float#
Returns a Float flag or argument value.
fn float(name String) FloatMatches.float_or_null#
Returns a Float value, or null when absent.
fn float_or_null(name String) Float?Matches.int#
Returns an Int flag or argument value.
fn int(name String) IntMatches.int_or_null#
Returns an Int value, or null when absent.
fn int_or_null(name String) Int?Matches.list#
Returns every value of a List flag or rest argument, in order; empty when absent.
fn list(name String) List[String]Matches.string#
Returns the text of a flag or argument.
fn string(name String) StringMatches.string_or_null#
Returns the text, or null for an optional argument or flag that was not supplied.
fn string_or_null(name String) String?Parsed Enum#
The outcome of parsing: help text to show, or a node ready to run.
enum Parsed {
Help(text String)
Run(matches Matches)
}Parsed.Help#
-h, --help, a leading help word, or a group invoked without a subcommand.
Help(text String)Parsed.Run#
A leaf to run: Command.run passes the values to the handler at matches.path.
Run(matches Matches)Parsed.Help.text#
The rendered help ending in a newline, ready to write to stdout.
text StringParsed.Run.matches#
The values to pass the handler; path names the command reached from the
root.
matches Matchescommand Function#
Builds a leaf command whose handler receives the options record T.
Each field of T is filled from the flag or argument with its name, dashes written as underscores, and has the
type its kind names:
| declared | field type |
|---|---|
FlagKind.Bool | Bool (absent is false) |
FlagKind.String | String |
FlagKind.Int | Int |
FlagKind.Float | Float |
FlagKind.List, and a rest argument | List[String] (absent is empty) |
Arg with kind Int or Float | Int or Float |
any other Arg | String |
A flag with neither a default nor required, and an optional Arg, needs an ...? field, which is null
when absent. A field with no matching value, or of the wrong type, fails the run with an Error with kind "EINVAL". The
handler returns the exit status.
fn command[T Data](move name String, move run fn(T) Result[Int, Error], move about String = "", move flags List[Flag] = [], move args List[Arg] = []) Command { ... }