Skip to main content

FromArgMatches

Trait FromArgMatches 

Source
pub trait FromArgMatches: Sized {
    // Required methods
    fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error>;
    fn update_from_arg_matches(
        &mut self,
        matches: &ArgMatches,
    ) -> Result<(), Error>;

    // Provided methods
    fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> { ... }
    fn update_from_arg_matches_mut(
        &mut self,
        matches: &mut ArgMatches,
    ) -> Result<(), Error> { ... }
}
Expand description

Converts an instance of ArgMatches to a user-defined container.

Derived as part of Parser, Args, and Subcommand.

Required Methods§

Source

fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed.

Motivation: If our application had two CLI options, --name <STRING> and the flag --debug, we may create a struct as follows:

struct Context {
    name: String,
    debug: bool
}

We then need to convert the ArgMatches that clap generated into our struct. from_arg_matches serves as the equivalent of:

impl From<ArgMatches> for Context {
   fn from(m: ArgMatches) -> Self {
       Context {
           name: m.get_one::<String>("name").unwrap().clone(),
           debug: m.get_flag("debug"),
       }
   }
}
Source

fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error>

Assign values from ArgMatches to self.

Provided Methods§

Source

fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed.

Motivation: If our application had two CLI options, --name <STRING> and the flag --debug, we may create a struct as follows:

struct Context {
    name: String,
    debug: bool
}

We then need to convert the ArgMatches that clap generated into our struct. from_arg_matches_mut serves as the equivalent of:

impl From<ArgMatches> for Context {
   fn from(m: ArgMatches) -> Self {
       Context {
           name: m.get_one::<String>("name").unwrap().to_string(),
           debug: m.get_flag("debug"),
       }
   }
}
Examples found in repository?
examples/derive_ref/flatten_hand_args.rs (line 14)
12    fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
13        let mut matches = matches.clone();
14        Self::from_arg_matches_mut(&mut matches)
15    }
Source

fn update_from_arg_matches_mut( &mut self, matches: &mut ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.

Examples found in repository?
examples/derive_ref/flatten_hand_args.rs (line 25)
23    fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
24        let mut matches = matches.clone();
25        self.update_from_arg_matches_mut(&mut matches)
26    }

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FromArgMatches for !

Source§

impl FromArgMatches for ()

Source§

impl<T> FromArgMatches for Box<T>
where T: FromArgMatches,

Implementors§