Macro cargo::util::command_prelude::value_parser
source · [−]macro_rules! value_parser {
($name:ty) => { ... };
}
Expand description
Select a ValueParser
implementation from the intended type
To register a custom type with this macro, implement ValueParserFactory
.
Example
Usage:
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("output")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "file.txt"]).unwrap();
let port: &PathBuf = m.get_one("output")
.expect("required");
assert_eq!(port, Path::new("file.txt"));
Supported types:
// Built-in types
let parser = clap::value_parser!(String);
assert_eq!(format!("{:?}", parser), "ValueParser::string");
let parser = clap::value_parser!(std::ffi::OsString);
assert_eq!(format!("{:?}", parser), "ValueParser::os_string");
let parser = clap::value_parser!(std::path::PathBuf);
assert_eq!(format!("{:?}", parser), "ValueParser::path_buf");
let parser = clap::value_parser!(u16).range(3000..);
assert_eq!(format!("{:?}", parser), "RangedI64ValueParser { bounds: (Included(3000), Included(65535)), target: PhantomData }");
let parser = clap::value_parser!(u64).range(3000..);
assert_eq!(format!("{:?}", parser), "RangedU64ValueParser { bounds: (Included(3000), Unbounded), target: PhantomData }");
// FromStr types
let parser = clap::value_parser!(usize);
assert_eq!(format!("{:?}", parser), "_AnonymousValueParser(ValueParser::other(usize))");
// ValueEnum types
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum ColorChoice {
Always,
Auto,
Never,
}
impl clap::ValueEnum for ColorChoice {
// ...
}
let parser = clap::value_parser!(ColorChoice);
assert_eq!(format!("{:?}", parser), "EnumValueParser(PhantomData)");