Trait clap::builder::TypedValueParser
source · pub trait TypedValueParser: Clone + Send + Sync + 'static {
type Value;
fn parse_ref(
&self,
cmd: &Command<'_>,
arg: Option<&Arg<'_>>,
value: &OsStr
) -> Result<Self::Value, Error>;
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§
Required Methods§
Provided Methods§
sourcefn parse(
&self,
cmd: &Command<'_>,
arg: Option<&Arg<'_>>,
value: OsString
) -> Result<Self::Value, Error>
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.
sourcefn possible_values(
&self
) -> Option<Box<dyn Iterator<Item = PossibleValue<'static>> + '_>>
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.
sourcefn map<T, F>(self, func: F) -> MapValueParser<Self, F>where
T: Send + Sync + Clone,
F: Fn(Self::Value) -> T + Clone,
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)
);