Trait clap::builder::TypedValueParser

source ·
pub trait TypedValueParser:
    Clone
    + Send
    + Sync
    + 'static {
    type Value;

    // Required method
    fn parse_ref(
        &self,
        cmd: &Command<'_>,
        arg: Option<&Arg<'_>>,
        value: &OsStr,
    ) -> Result<Self::Value, Error>;

    // Provided methods
    fn parse(
        &self,
        cmd: &Command<'_>,
        arg: Option<&Arg<'_>>,
        value: OsString,
    ) -> Result<Self::Value, Error> { ... }
    fn possible_values(
        &self,
    ) -> Option<Box<dyn Iterator<Item = PossibleValue<'static>> + '_>> { ... }
    fn map<T, F>(self, func: F) -> MapValueParser<Self, F>
       where T: Send + Sync + Clone,
             F: Fn(Self::Value) -> T + Clone { ... }
}
Expand description

Parse/validate argument values

Required Associated Types§

source

type Value

Argument’s value type

Required Methods§

source

fn parse_ref( &self, cmd: &Command<'_>, arg: Option<&Arg<'_>>, value: &OsStr, ) -> Result<Self::Value, Error>

Parse the argument value

When arg is None, an external subcommand value is being parsed.

Provided Methods§

source

fn parse( &self, cmd: &Command<'_>, arg: Option<&Arg<'_>>, value: OsString, ) -> Result<Self::Value, Error>

Parse the argument value

When arg is None, an external subcommand value is being parsed.

source

fn possible_values( &self, ) -> Option<Box<dyn Iterator<Item = PossibleValue<'static>> + '_>>

Reflect on enumerated value properties

Error checking should not be done with this; it is mostly targeted at user-facing applications like errors and completion.

source

fn map<T, F>(self, func: F) -> MapValueParser<Self, F>
where T: Send + Sync + Clone, F: Fn(Self::Value) -> T + Clone,

Adapt a TypedValueParser from one value to another

§Example
let cmd = Command::new("mycmd")
    .arg(
        Arg::new("flag")
            .long("flag")
            .action(clap::ArgAction::Set)
            .value_parser(
                BoolishValueParser::new()
                .map(|b| -> usize {
                    if b { 10 } else { 5 }
                })
            )
    );

let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag=true", "--flag=true"]).unwrap();
assert!(matches.contains_id("flag"));
assert_eq!(
    matches.get_one::<usize>("flag").copied(),
    Some(10)
);

let matches = cmd.try_get_matches_from(["mycmd", "--flag=false"]).unwrap();
assert!(matches.contains_id("flag"));
assert_eq!(
    matches.get_one::<usize>("flag").copied(),
    Some(5)
);

Object Safety§

This trait is not object safe.

Implementors§