Struct CommandMap

Source
pub struct CommandMap<'a, T = (), E = Box<dyn Error>> { /* private fields */ }
Expand description

A type which has a set of ActionCommands 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>

Source

pub fn builder() -> CommandMapBuilder<'a>

Creates a builder type which is used to tell the CommandMap about the ActionCommands it will be mapping over.

Source

pub fn commands(&self) -> Vec<Command>

The Clap Commands for this CommandMap. Use this with clap::Command::subcommands to configure it to use this CommandMap.

Source

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 CommandMaps it is helpful to keep the previous subcommand stack accessible by extending the matches vector using vec1::Vec1::from_vec_push.

Auto Trait Implementations§

§

impl<'a, T, E> Freeze for CommandMap<'a, T, E>

§

impl<'a, T = (), E = Box<dyn Error>> !RefUnwindSafe for CommandMap<'a, T, E>

§

impl<'a, T, E> Send for CommandMap<'a, T, E>

§

impl<'a, T, E> Sync for CommandMap<'a, T, E>

§

impl<'a, T, E> Unpin for CommandMap<'a, T, E>

§

impl<'a, T = (), E = Box<dyn Error>> !UnwindSafe for CommandMap<'a, T, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.