pub struct CommandMap<'a, T = (), E = Box<dyn Error>> { /* private fields */ }
Expand description
A type which has a set of ActionCommand
s and can provide the
Clap Command
for command line arg parsing, as well as map a
matched Command
back to its ActionCommand
for dispatch to
its action function.
// Add commands to the map by pushing ActionCommand trait objects
// onto the map
let command_map = CommandMap::builder()
.push(HelloWorldCommand {})
.build();
// Tell the relevant Clap Command about all the subcommands
let command = Command::new("my-program")
.subcommands(command_map.commands());
let matches = command.get_matches_from(
["my-program", "hello-world"]
);
// Dispatch to the relevant subcommand, saving some if/else-if
// chains
command_map.dispatch(vec1![&matches]);
This type can be composed, for example on a subcommand with multiple
subcommands of its own. See CommandMapActionCommand
for a
minimal example.
Implementations§
Source§impl<'a> CommandMap<'a>
impl<'a> CommandMap<'a>
Sourcepub fn builder() -> CommandMapBuilder<'a>
pub fn builder() -> CommandMapBuilder<'a>
Creates a builder type which is used to tell the CommandMap
about the ActionCommand
s it will be mapping over.
Sourcepub fn commands(&self) -> Vec<Command>
pub fn commands(&self) -> Vec<Command>
The Clap Command
s for this CommandMap
. Use this with
clap::Command::subcommands
to configure it to use this
CommandMap
.
Sourcepub fn dispatch(&self, matches: Vec1<&ArgMatches>) -> Result<(), DispatchError>
pub fn dispatch(&self, matches: Vec1<&ArgMatches>) -> Result<(), DispatchError>
Dispatch this CommandMap
using ArgMatches
.
When starting from scratch simply construct a new
vec1::Vec1
with a single clap::ArgMatches
in it; when
nesting multiple CommandMap
s it is helpful to keep the
previous subcommand stack accessible by extending the matches
vector using vec1::Vec1::from_vec_push
.