Struct pretty_exec::clap::builder::ValueParser
[−]pub struct ValueParser(_);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 type ValueParser::newfor additionalTypedValueParserthat 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())
.takes_value(true)
.required(true)
)
.arg(
clap::Arg::new("port")
.long("port")
.value_parser(clap::value_parser!(u16).range(3000..))
.takes_value(true)
.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
impl ValueParser
impl ValueParser
pub fn new<P>(other: P) -> ValueParser where
P: TypedValueParser,
<P as TypedValueParser>::Value: Send,
<P as TypedValueParser>::Value: Sync,
<P as TypedValueParser>::Value: Clone,
pub fn new<P>(other: P) -> ValueParser where
P: TypedValueParser,
<P as TypedValueParser>::Value: Send,
<P as TypedValueParser>::Value: Sync,
<P as TypedValueParser>::Value: Clone,
Custom parser for argument values
To create a custom parser, see TypedValueParser
Pre-existing implementations include:
EnumValueParserandPossibleValuesParserfor static enumerated valuesBoolishValueParserandFalseyValueParserfor alternativeboolimplementationsRangedI64ValueParserandRangedU64ValueParserNonEmptyStringValueParser
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())));pub const fn bool() -> ValueParser
pub const fn bool() -> ValueParser
bool parser for argument values
See also:
BoolishValueParserfor different human readable bool representationsFalseyValueParserfor 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());pub 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");pub 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]);pub 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());impl ValueParser
impl ValueParser
pub fn type_id(&self) -> AnyValueId
pub fn type_id(&self) -> AnyValueId
Describes the content of AnyValue
pub fn possible_values(
&self
) -> Option<Box<dyn Iterator<Item = PossibleValue<'static>>, Global>>
pub fn possible_values(
&self
) -> Option<Box<dyn Iterator<Item = PossibleValue<'static>>, Global>>
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
impl Clone for ValueParser
impl Clone for ValueParser
fn clone(&self) -> ValueParser
fn clone(&self) -> ValueParser
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
impl Debug for ValueParser
impl Debug for ValueParser
impl<P, const C: usize> From<[P; C]> for ValueParser where
P: Into<PossibleValue<'static>>,
impl<P, const C: usize> From<[P; C]> for ValueParser where
P: Into<PossibleValue<'static>>,
Create a ValueParser with PossibleValuesParser
See PossibleValuesParser for more flexibility in creating the
PossibleValues.
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");fn from(values: [P; C]) -> ValueParser
fn from(values: [P; C]) -> ValueParser
Converts to this type from the input type.
impl<P> From<P> for ValueParser where
P: 'static + TypedValueParser + Send + Sync,
<P as TypedValueParser>::Value: Send,
<P as TypedValueParser>::Value: Sync,
<P as TypedValueParser>::Value: Clone,
impl<P> From<P> for ValueParser where
P: 'static + TypedValueParser + Send + Sync,
<P as TypedValueParser>::Value: Send,
<P as TypedValueParser>::Value: Sync,
<P as TypedValueParser>::Value: Clone,
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())
.takes_value(true)
.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");fn from(p: P) -> ValueParser
fn from(p: P) -> ValueParser
Converts to this type from the input type.
impl From<Range<i64>> for ValueParser
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)
.takes_value(true)
.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);fn from(value: Range<i64>) -> ValueParser
fn from(value: Range<i64>) -> ValueParser
Converts to this type from the input type.
impl From<RangeFrom<i64>> for ValueParser
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..)
.takes_value(true)
.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);fn from(value: RangeFrom<i64>) -> ValueParser
fn from(value: RangeFrom<i64>) -> ValueParser
Converts to this type from the input type.
impl From<RangeFull> for ValueParser
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(..)
.takes_value(true)
.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);fn from(value: RangeFull) -> ValueParser
fn from(value: RangeFull) -> ValueParser
Converts to this type from the input type.
impl From<RangeInclusive<i64>> for ValueParser
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)
.takes_value(true)
.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);fn from(value: RangeInclusive<i64>) -> ValueParser
fn from(value: RangeInclusive<i64>) -> ValueParser
Converts to this type from the input type.
impl From<RangeTo<i64>> for ValueParser
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)
.takes_value(true)
.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);fn from(value: RangeTo<i64>) -> ValueParser
fn from(value: RangeTo<i64>) -> ValueParser
Converts to this type from the input type.
impl From<RangeToInclusive<i64>> for ValueParser
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)
.takes_value(true)
.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);fn from(value: RangeToInclusive<i64>) -> ValueParser
fn from(value: RangeToInclusive<i64>) -> ValueParser
Converts to this type from the input type.
Auto Trait Implementations
impl !RefUnwindSafe for ValueParser
impl Send for ValueParser
impl Sync for ValueParser
impl Unpin for ValueParser
impl !UnwindSafe for ValueParser
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<X> Pipe for X
impl<X> Pipe for X
fn pipe<Return, Function>(self, f: Function) -> Return where
Function: FnOnce(Self) -> Return,
fn pipe<Return, Function>(self, f: Function) -> Return where
Function: FnOnce(Self) -> Return,
Apply f to self. Read more
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Return where
Function: FnOnce(&'a Self) -> Return,
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Return where
Function: FnOnce(&'a Self) -> Return,
Apply f to &self. Read more
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Return where
Function: FnOnce(&'a mut Self) -> Return,
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Return where
Function: FnOnce(&'a mut Self) -> Return,
Apply f to &mut self. Read more
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: AsRef<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: AsRef<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait AsRef<Param>. Read more
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return where
Self: AsMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return where
Self: AsMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait AsMut<Param>. Read more
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Deref<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Deref<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait Deref<Target = Param>. Read more
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait [DerefMut<Target = Param>]. Read more
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Borrow<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return where
Self: Borrow<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a Param) -> Return,
Apply f to &self where f takes a single parameter of type Param
and Self implements trait Borrow<Param>. Read more
fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: BorrowMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function
) -> Return where
Self: BorrowMut<Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply f to &mut self where f takes a single parameter of type Param
and Self implements trait BorrowMut<Param>. Read more