View Source cli behaviour (argparse v2.0.0)
Command line utility behaviour. Usage example:
From an escript main/1 function (requires-mode(compile)
): cli:run(Args).
Or, to limit cli behaviour discovery, cli:run(Args, #{modules => ?MODULE, progname => ?MODULE}).
Other options available for run/2:modules
:all_loaded
- search all loaded modules (code:all_loaded/0
) forcli
behaviourmodule()
- use this module (must exportcli/0
)- [module()] - list of modules (must export
cli/0
)
warn
: set tosuppress
suppresses warnings loggederror
: defines what action is taken upon parser error. Useok
to completely ignore the error (historical behaviour, useful for testing),error
to raise an exception,halt
to halt the emulator with exit code 1 (default behaviour), and{halt, non_neg_integer()}
for a custom exit code halting the emulatorhelp
: set to false suppresses printingusage
when parser produces an error, and disables default --help/-h behaviourprefixes
: prefixes passed to argparseprogname
: specifies executable name instead of 'erl'
Warnings are printed to OTP logger, unless suppressed.
cli framework attempts to create a handler for each command exported, including intermediate (non-leaf) commands, if it can find function exported with suitable signature.
cli examples are available on GitHubLink to this section Summary
Functions
Equivalent to run(Args, #{}).
CLI entry point, parses arguments and executes selected function. Finds all modules loaded, and implementing cli behaviour, then matches a command and runs handler defined for a command.
Link to this section Types
-type run_options() ::
#{modules => all_loaded | module() | [module()],
warn => suppress | warn,
help => boolean(),
error => ok | error | halt | {halt, non_neg_integer()},
prefixes => [integer()],
default => term(),
progname => string() | atom()}.
Link to this section Callbacks
-callback cli() -> args:command().
Link to this section Functions
-spec run(Args :: [string()]) -> term().
Equivalent to run(Args, #{}).
-spec run([string()], run_options()) -> term().