Module otpbp_argparse

Data Types

arg_map()

arg_map() = #{argument_name() => term()}

Arguments map: argument name to a term, produced by parser. Supplied to the command handler

arg_type()

arg_type() = boolean | float | {float, Choice::[float()]} | {float, [{min, float()} | {max, float()}]} | integer | {integer, Choices::[integer()]} | {integer, [{min, integer()} | {max, integer()}]} | string | {string, Choices::[string()]} | {string, Re::string()} | {string, Re::string(), ReOptions::[term()]} | binary | {binary, Choices::[binary()]} | {binary, Re::binary()} | {binary, Re::binary(), ReOptions::[term()]} | atom | {atom, Choices::[atom()]} | {atom, unsafe} | {custom, fun((string()) -> term())}

Built-in types include basic validation abilities String and binary validation may use regex match (ignoring captured value). For float, integer, string, binary and atom type, it is possible to specify available choices instead of regex/min/max.

argument()

argument() = #{name := argument_name(), short => char(), long => string(), required => boolean(), default => term(), type => arg_type(), action => store | {store, term()} | append | {append, term()} | count | extend, nargs => pos_integer() | 'maybe' | {'maybe', term()} | list | nonempty_list | all, help => hidden | unicode:chardata() | argument_help()}

argument_help()

argument_help() = {unicode:chardata(), [unicode:chardata() | type | default] | fun(() -> unicode:chardata())}

Help template definition for argument. Short and long forms exist for every argument. Short form is printed together with command definition, e.g. "usage: rm [--force]", while long description is printed in detailed section below: "--force forcefully remove".

argument_name()

argument_name() = atom() | string() | binary()

cmd_path()

cmd_path() = [string()]

Command path, for nested commands

command()

command() = #{commands => #{string() => command()}, arguments => [argument()], help => hidden | unicode:chardata() | command_help(), handler => handler()}

command_help()

command_help() = [unicode:chardata() | usage | commands | arguments | options]

Template for the command help/usage message.

handler()

handler() = optional | fun((arg_map()) -> term()) | {module(), Fn::atom()} | {fun(() -> term()), term()} | {module(), atom(), term()}

parse_result()

parse_result() = {ok, arg_map(), Path::cmd_path(), command()} | {error, parser_error()}

Parser result: argument map, path leading to successfully matching command (contains only ["progname"] if there were no subcommands matched), and a matching command.

parser_error()

parser_error() = {Path::cmd_path(), Expected::argument() | undefined, Actual::string() | undefined, Details::unicode:chardata()}

Returned from parse/2,3 when command spec is valid, but the command line cannot be parsed using the spec. When Expected is undefined, but Actual is not, it means that the input contains an unexpected argument which cannot be parsed according to command spec. When Expected is an argument, and Actual is undefined, it means that a mandatory argument is not provided in the command line. When both Expected and Actual are defined, it means that the supplied argument is failing validation. When both are undefined, there is some logical issue (e.g. a sub-command is required, but was not selected).

parser_options()

parser_options() = #{prefixes => [char()], default => term(), progname => string() | atom(), command => cmd_path(), columns => pos_integer()}

validator_error()

validator_error() = {otpbp_argparse, command | argument, cmd_path(), Field::atom(), Detail::unicode:chardata()}

Function Index

format_error/1Basic formatter for the parser error reason.
format_error/2Transforms exception thrown by validate/1,2 according to EEP54.
help/1Equivalent to help(Command, #{}).
help/2Returns help for Command formatted according to Options specified.
parse/2Equivalent to parse(Args, Command, #{}).
parse/3Parses supplied arguments according to expected command specification.
run/3
validate/1Equivalent to validate(Command, #{}).
validate/2Validate command specification, taking Options into account.

Function Details

format_error/1

format_error(Reason::parser_error()) -> unicode:chardata()

Basic formatter for the parser error reason.

format_error/2

format_error(Reason::validator_error(), X2::erlang:stacktrace()) -> map()

Transforms exception thrown by validate/1,2 according to EEP54. Use erl_error:format_exception/3,4 to get the shell-like output.

help/1

help(Command::command()) -> string()

Equivalent to help(Command, #{}).

help/2

help(Command::command(), Options::parser_options()) -> unicode:chardata()

Returns help for Command formatted according to Options specified

parse/2

parse(Args::[string()], Command::command()) -> parse_result()

Equivalent to parse(Args, Command, #{}).

parse/3

parse(Args::[string()], Command::command(), Options::parser_options()) -> parse_result()

Args: command line arguments (e.g. init:get_plain_arguments())

returns: argument map, or argument map with deepest matched command definition.

Parses supplied arguments according to expected command specification.

run/3

run(Args::[string()], Command::command(), Options::parser_options()) -> term()

validate/1

validate(Command::command()) -> Progname::string()

Equivalent to validate(Command, #{}).

validate/2

validate(Command::command(), Options::parser_options()) -> Progname::string()

Validate command specification, taking Options into account. Raises an error if the command specification is invalid.


Generated by EDoc