Macro clap::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..);
let _parser = clap::value_parser!(u64).range(3000..);
// 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);