pub struct ValueParser(/* private fields */);
Expand description
Parse/validate argument values
Specified with Arg::value_parser
.
ValueParser
defines how to convert a raw argument value into a validated and typed value for
use within an application.
See
value_parser!
for automatically selecting an implementation for a given typeValueParser::new
for additionalTypedValueParser
that can be used
§Example
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("color")
.long("color")
.value_parser(["always", "auto", "never"])
.default_value("auto")
)
.arg(
clap::Arg::new("hostname")
.long("hostname")
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.action(clap::ArgAction::Set)
.required(true)
)
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(clap::value_parser!(u16).range(3000..))
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(
["cmd", "--hostname", "rust-lang.org", "--port", "3001"]
).unwrap();
let color: &String = m.get_one("color")
.expect("default");
assert_eq!(color, "auto");
let hostname: &String = m.get_one("hostname")
.expect("required");
assert_eq!(hostname, "rust-lang.org");
let port: u16 = *m.get_one("port")
.expect("required");
assert_eq!(port, 3001);
Implementations§
Source§impl ValueParser
impl ValueParser
Sourcepub fn new<P>(other: P) -> ValueParserwhere
P: TypedValueParser,
pub fn new<P>(other: P) -> ValueParserwhere
P: TypedValueParser,
Custom parser for argument values
Pre-existing TypedValueParser
implementations include:
Fn(&str) -> Result<T, E>
EnumValueParser
andPossibleValuesParser
for static enumerated valuesBoolishValueParser
andFalseyValueParser
for alternativebool
implementationsRangedI64ValueParser
andRangedU64ValueParser
NonEmptyStringValueParser
§Example
type EnvVar = (String, Option<String>);
fn parse_env_var(env: &str) -> Result<EnvVar, std::io::Error> {
if let Some((var, value)) = env.split_once('=') {
Ok((var.to_owned(), Some(value.to_owned())))
} else {
Ok((env.to_owned(), None))
}
}
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("env")
.value_parser(clap::builder::ValueParser::new(parse_env_var))
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "key=value"]).unwrap();
let port: &EnvVar = m.get_one("env")
.expect("required");
assert_eq!(*port, ("key".into(), Some("value".into())));
Sourcepub const fn bool() -> ValueParser
pub const fn bool() -> ValueParser
bool
parser for argument values
See also:
BoolishValueParser
for different human readable bool representationsFalseyValueParser
for assuming non-false is true
§Example
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("download")
.value_parser(clap::value_parser!(bool))
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "true"]).unwrap();
let port: bool = *m.get_one("download")
.expect("required");
assert_eq!(port, true);
assert!(cmd.try_get_matches_from_mut(["cmd", "forever"]).is_err());
Sourcepub const fn string() -> ValueParser
pub const fn string() -> ValueParser
String
parser for argument values
See also:
§Example
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.value_parser(clap::value_parser!(String))
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "80"]).unwrap();
let port: &String = m.get_one("port")
.expect("required");
assert_eq!(port, "80");
Sourcepub const fn os_string() -> ValueParser
pub const fn os_string() -> ValueParser
OsString
parser for argument values
§Example
use std::ffi::OsString;
use std::os::unix::ffi::{OsStrExt,OsStringExt};
let r = Command::new("myprog")
.arg(
Arg::new("arg")
.required(true)
.value_parser(ValueParser::os_string())
)
.try_get_matches_from(vec![
OsString::from("myprog"),
OsString::from_vec(vec![0xe9])
]);
assert!(r.is_ok());
let m = r.unwrap();
let arg: &OsString = m.get_one("arg")
.expect("required");
assert_eq!(arg.as_bytes(), &[0xe9]);
Sourcepub const fn path_buf() -> ValueParser
pub const fn path_buf() -> ValueParser
PathBuf
parser for argument values
§Example
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", "hello.txt"]).unwrap();
let port: &PathBuf = m.get_one("output")
.expect("required");
assert_eq!(port, Path::new("hello.txt"));
assert!(cmd.try_get_matches_from_mut(["cmd", ""]).is_err());
Source§impl ValueParser
impl ValueParser
Sourcepub fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>>
pub fn possible_values( &self, ) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>>
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.
Trait Implementations§
Source§impl Clone for ValueParser
impl Clone for ValueParser
Source§fn clone(&self) -> ValueParser
fn clone(&self) -> ValueParser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ValueParser
impl Debug for ValueParser
Source§impl<P, const C: usize> From<[P; C]> for ValueParserwhere
P: Into<PossibleValue>,
Create a ValueParser
with PossibleValuesParser
impl<P, const C: usize> From<[P; C]> for ValueParserwhere
P: Into<PossibleValue>,
Create a ValueParser
with PossibleValuesParser
See PossibleValuesParser
for more flexibility in creating the
PossibleValue
s.
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("color")
.long("color")
.value_parser(["always", "auto", "never"])
.default_value("auto")
);
let m = cmd.try_get_matches_from_mut(
["cmd", "--color", "never"]
).unwrap();
let color: &String = m.get_one("color")
.expect("default");
assert_eq!(color, "never");
Source§fn from(values: [P; C]) -> ValueParser
fn from(values: [P; C]) -> ValueParser
Source§impl<P> From<P> for ValueParser
Convert a TypedValueParser
to ValueParser
impl<P> From<P> for ValueParser
Convert a TypedValueParser
to ValueParser
§Example
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("hostname")
.long("hostname")
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(
["cmd", "--hostname", "rust-lang.org"]
).unwrap();
let hostname: &String = m.get_one("hostname")
.expect("required");
assert_eq!(hostname, "rust-lang.org");
Source§fn from(p: P) -> ValueParser
fn from(p: P) -> ValueParser
Source§impl From<Range<i64>> for ValueParser
Create an i64
ValueParser
from a N..M
range
impl From<Range<i64>> for ValueParser
Create an i64
ValueParser
from a N..M
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(3000..4000)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 3001);
Source§impl From<RangeFrom<i64>> for ValueParser
Create an i64
ValueParser
from a N..
range
impl From<RangeFrom<i64>> for ValueParser
Create an i64
ValueParser
from a N..
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(3000..)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 3001);
Source§impl From<RangeFull> for ValueParser
Create an i64
ValueParser
from a ..
range
impl From<RangeFull> for ValueParser
Create an i64
ValueParser
from a ..
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(..)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 3001);
Source§fn from(value: RangeFull) -> ValueParser
fn from(value: RangeFull) -> ValueParser
Source§impl From<RangeInclusive<i64>> for ValueParser
Create an i64
ValueParser
from a N..=M
range
impl From<RangeInclusive<i64>> for ValueParser
Create an i64
ValueParser
from a N..=M
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(3000..=4000)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 3001);
Source§fn from(value: RangeInclusive<i64>) -> ValueParser
fn from(value: RangeInclusive<i64>) -> ValueParser
Source§impl From<RangeTo<i64>> for ValueParser
Create an i64
ValueParser
from a ..M
range
impl From<RangeTo<i64>> for ValueParser
Create an i64
ValueParser
from a ..M
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(..3000)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "80"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 80);
Source§impl From<RangeToInclusive<i64>> for ValueParser
Create an i64
ValueParser
from a ..=M
range
impl From<RangeToInclusive<i64>> for ValueParser
Create an i64
ValueParser
from a ..=M
range
See RangedI64ValueParser
for more control over the output type.
See also RangedU64ValueParser
§Examples
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(..=3000)
.action(clap::ArgAction::Set)
.required(true)
);
let m = cmd.try_get_matches_from_mut(["cmd", "--port", "80"]).unwrap();
let port: i64 = *m.get_one("port")
.expect("required");
assert_eq!(port, 80);
Source§fn from(value: RangeToInclusive<i64>) -> ValueParser
fn from(value: RangeToInclusive<i64>) -> ValueParser
Source§impl<P> From<Vec<P>> for ValueParserwhere
P: Into<PossibleValue>,
Create a ValueParser
with PossibleValuesParser
impl<P> From<Vec<P>> for ValueParserwhere
P: Into<PossibleValue>,
Create a ValueParser
with PossibleValuesParser
See PossibleValuesParser
for more flexibility in creating the
PossibleValue
s.
§Examples
let possible = vec!["always", "auto", "never"];
let mut cmd = clap::Command::new("raw")
.arg(
clap::Arg::new("color")
.long("color")
.value_parser(possible)
.default_value("auto")
);
let m = cmd.try_get_matches_from_mut(
["cmd", "--color", "never"]
).unwrap();
let color: &String = m.get_one("color")
.expect("default");
assert_eq!(color, "never");
Source§fn from(values: Vec<P>) -> ValueParser
fn from(values: Vec<P>) -> ValueParser
Source§impl IntoResettable<ValueParser> for Option<ValueParser>
impl IntoResettable<ValueParser> for Option<ValueParser>
Source§fn into_resettable(self) -> Resettable<ValueParser>
fn into_resettable(self) -> Resettable<ValueParser>
Auto Trait Implementations§
impl Freeze for ValueParser
impl !RefUnwindSafe for ValueParser
impl Send for ValueParser
impl Sync for ValueParser
impl Unpin for ValueParser
impl !UnwindSafe for ValueParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<I> IntoResettable<ValueParser> for Iwhere
I: Into<ValueParser>,
impl<I> IntoResettable<ValueParser> for Iwhere
I: Into<ValueParser>,
Source§fn into_resettable(self) -> Resettable<ValueParser>
fn into_resettable(self) -> Resettable<ValueParser>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more