A behaviour to define module-based commands.
A command is a keyword list (or a module implementing this behaviour) and accepts these top-level entries:
:options— option schema (seeCliMate.CLI.Option).:arguments— positional argument schema.:subcommands— a keyword list of nested commands. A command cannot declare both:argumentsand:subcommands; the first positional slot is consumed as the sub-command name. A sub-command value can be an inline keyword list, or a module implementing this behaviour.:execute— a 1-arity function called with the parsed result. Also provided via theexecute/1callback on module-based commands. When a command with:executeis selected,CliMate.CLI.parse/2returns the parsed map with an:executekey holding a zero-arity closure; calling it runs the function with the parsed map (minus the:executekey). Returnsnilwhen no execute is defined or when--helpwas requested.:name,:version,:doc— metadata used byformat_usage/2.
Option inheritance across sub-commands
Options declared on a parent command are inherited by sub-commands and can be
passed on any level where the command is being parsed. Merging rules for the
final :options map:
- A value explicitly parsed from argv at any level wins and is never overwritten by a default from a deeper level.
- If an option is not passed on argv, the default from the deepest level that
declares the option is used. Redefining an option at a child level replaces
the parent entry entirely (including
:shortand:keep). :keeplists are not accumulated across levels: the list parsed at a given level replaces any previously accumulated list.
Summary
Callbacks
Returns a command definition to be used with the parser, or invoked as a sub command.
Types
@type argument() :: [argument_opt()]
@type argument_opt() :: {:key, atom()} | {:required, boolean()} | {:type, CliMate.CLI.Argument.vtype()} | {:doc, binary() | nil} | {:cast, nil | CliMate.CLI.Argument.caster()}
@type command() :: [command_opt()]
@type option() :: [option_opt()]
@type t() :: %CliMate.CLI.Command{ arguments: [CliMate.CLI.Argument.t()], doc: binary() | nil, execute: (-> term()) | nil, module: module() | nil, name: binary() | nil, options: [{atom(), CliMate.CLI.Option.t()}], subcommands: [t()], version: binary() | nil }
Callbacks
@callback command() :: command()
Returns a command definition to be used with the parser, or invoked as a sub command.
@callback execute(CliMate.CLI.parsed()) :: term()