On this page:
parser
parse
flag
multi
once_  each
once_  any
args
state
help
8.16.0.2
14.6.1 Command Line Parser Construction🔗ℹ

expression

cmdline.parser:

  option

  ...

  content_expr

  ...

 

expression

cmdline.parse:

  option

  ...

  content_expr

  ...

 

option

 = 

~init expr

 | 

~init: body; ...

 | 

~no_builtin

The cmdline.parser form produces a Parser, where the content_expr expressions produce flag and argument handlers, especially using cmdline.flag and cmdline.args. The cmdline.parse form has the same syntax to create the same Parser, but it immediately calls the Parser.parse method on the resulting Parser object.

The Parser.parse method returns a map representing the final parse state. If ~init is provided as an option form, the result of the body sequence produces a map to serve as the parser’s initial state.

Unless ~no_builtin is provided as an option, then --help/-h and -- flag support is implicitly added to the content_exprs.

The result of content_expr must be either

expression

cmdline.flag flag_string arg ... maybe_ellipsis

 

expression

cmdline.flag flag_string arg ... maybe_ellipsis:

  option

  ...

  body

  ...

 

arg

 = 

identifier

 | 

(identifier :: annot)

 | 

(identifier as identifier)

 | 

(identifier as identifier :: annot)

 

option

 = 

~alias flag_string ...

 | 

~alias: flag_string ...; ...

 | 

~help expr

 | 

~help: body; ...

 | 

~key expr

 | 

~key: body; ...

 | 

~init expr

 | 

~init: body; ...

 | 

~multi

 | 

~final

 

maybe_ellipsis

 = 

...

 | 

ϵ

Creates a Flag object to describe the handling of a command-line flag for cmdline.parse and related forms. The only required component is a flag_string, which must be a literal string starting with either - or +. The flag_string content must either continue with the same - or + character followed by one or more characters (for a long-form flag), or that has a single character after the leading - or + (for a short-form flag).

Arguments to the flag are described by subsequent args. If maybe_ellipsis after the args is ..., then the last arg can be repeated any number of times, and its binding is a repetition. When an arg has as, then the identifier before as is used as the argument name in help text, while the identifier after as is bound to the argument value for use in a body sequence at the end of the cmdline.flag body. If annot is included, it should recognize allowed strings, and it might convert an allowed string to a different representation like the String.to_int annotation.

After options, an optional body sequence can be provided. If this body sequence is non-empty, then it is responsible for updating the parser’s state via cmdline.state; the result of the body sequence is ignored. If the body sequence is empty, then a body is created automatically:

The ~alias option can provide additional flag_strings that serve as an alias for the flag. Typically, the main flag is short-form and an alias is long-form or vice versa.

The ~help option provides text to show with the flag in help output. The string produced by the expr or body sequence in ~help can contain newline characters, and space is added to the start of lines as needed.

The ~key option specifies a key used for a default flag body, if one is created. The key is not used if the cmdline.flag form has a non-empty body sequence.

The ~init option provides an expr or body sequence that must produce a map. The keys and values of the map are added to the parser’s initial state map. Keys must be distinct across all flag handlers for a parser.

The ~multi option causes a parser using the flag to allow multiple instances of the flag in a command line. If ~multi is not present, but the the cmdline.flag form is syntactically within a cmdline.multi form, then ~multi mode is automatically enabled. Otherwise, the flag can appear at most once within a command line. A flag without ~multi can be used in cmdline.multi to allow it multiple times, but the default flag body sequence (if generated) depends only on whether the ~multi option is present or a syntactically enclosing cmdline.multi form is present.

The ~final option causes all arguments that appears after he flag to be treated as non-flag arguments. This is the behavior of the builtin -- flag, and it is rarely needed for other flags.

expression

cmdline.multi:

  content_expr

  ...

 

expression

cmdline.once_each:

  content_expr

  ...

 

expression

cmdline.once_any:

  content_expr

  ...

Combines a set of flags and constrains the way that the flags can appear in a command line. Flags grouped with cmdline.multi can be used any number of times. Flags grouped with cmdline.once_each can be used a single time each. Flags grouped with cmdline.once_any can be used a single time and only when no other flag in the same set is used.

The result of a content_expr can be a Flag, as usually produced by cmdline.flag, a set of flags that already have the new set’s property, or a list of otherwise acceptable content_expr results (including nested lists).

expression

cmdline.args arg ... maybe_ellipsis

 

expression

cmdline.args arg ... maybe_ellipsis:

  option

  ...

  body

  ...

 

option

 = 

~init expr

 | 

~init: body; ...

Like cmdline.flag, but creates a Handler object to describe the handling of arguments after command-line flags for cmdline.parse and related forms.

The set of options for cmdline.args is more limited than cmdline.flags, since it does not include flag string or flag-specific help text.

The body sequence implements argument parsing the same way as for cmdline.flag. If the body sequence is empty, the default implementation always maps #'args to a list of arguments, even if that list is always empty or always contains only one item.

expression

cmdline.state

 

expression

cmdline.state := expr

 

expression

cmdline.state[key_expr] := val_expr

For use with a cmdline.flag or cmdline.args body sequence, accesses or updates the parser’s state, which is represented as a map.

A parser state map is immutable, but cmdline.state acts like a mutable variable. The mutable variable backing cmdline.state is specific to one evaluation of a handler body, so mutations to cmdline.state after the handler body returns cannot affect the result state of a parse.

The cmdline.state[key_expr] := val_expr form is allowed even though cmdline.state refers to an immutable map. It is a shorthand for an assignment to cmdline.state:

cmdline.state := cmdline.state ++ { key_expr: val_expr }

expression

cmdline.help:

  option

  ...

  body

  ...

 

option

 = 

placement

 

placement

 = 

~after_options

 | 

~after_notation

Creates a Text object to provide help text for cmdline.parse and related forms. The body sequence must produce a string. The string can contain newline characters to create multiline help text.

Helps text can be intermingled with flags in a form like cmdline.parse, and the text will be rendered in the same relative order as the flag and help-text implementations.

If ~after_options is provided as an option and the help text is after all flags and other help text, then it is placed after the builtin flags when those flags are not disabled (e.g., using ~no_builtin in cmdline.parse). If ~after_notation is provided as an option, then the help text is place even later, after a description of flag notation. Without ~after_options or ~after_options, trailing help text is rendered after all other text and flags, but before the help text for builtin options.