Macro clap::value_parser
source · macro_rules! value_parser {
($name:ty) => { ... };
}
Expand description
Select a ValueParser
implementation from the intended type
Supported types
ValueParserFactory
types, including- Native types:
bool
,String
,OsString
,PathBuf
- Ranged numeric types:
u8
,i8
,u16
,i16
,u32
,i32
,u64
,i64
- Native types:
ValueEnum
typesFrom<OsString>
types andFrom<&OsStr>
typesFrom<String>
types andFrom<&str>
typesFromStr
types, including usize, isize
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"));
Example mappings:
// 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
let parser = clap::value_parser!(ColorChoice);
assert_eq!(format!("{:?}", parser), "EnumValueParser(PhantomData)");