Struct ValueParser
pub struct ValueParser(/* private field */);
Parse/validate argument values
Specified with [Arg::value_parser][crate::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!][crate::value_parser] for automatically selecting an implementation for a given type ValueParser::newfor additionalTypedValueParserthat can be used
Example
# use clap_builder as clap;
let mut cmd = new
.arg
.arg
.arg;
let m = cmd.try_get_matches_from_mut.unwrap;
let color: &String = m.get_one
.expect;
assert_eq!;
let hostname: &String = m.get_one
.expect;
assert_eq!;
let port: u16 = *m.get_one
.expect;
assert_eq!;
Implementations
impl ValueParser
fn new<P>(other: P) -> Self where P: TypedValueParser,Custom parser for argument values
Pre-existing
TypedValueParserimplementations include:Fn(&str) -> Result<T, E>EnumValueParserandPossibleValuesParserfor static enumerated valuesBoolishValueParserandFalseyValueParserfor alternativeboolimplementationsRangedI64ValueParserandRangedU64ValueParserNonEmptyStringValueParser
Example
# use clap_builder as clap; type EnvVar = ; let mut cmd = new .arg; let m = cmd.try_get_matches_from_mut.unwrap; let port: &EnvVar = m.get_one .expect; assert_eq!;const fn bool() -> Selfboolparser for argument valuesSee also:
BoolishValueParserfor different human readable bool representationsFalseyValueParserfor assuming non-false is true
Example
# use clap_builder as clap; let mut cmd = new .arg; let m = cmd.try_get_matches_from_mut.unwrap; let port: bool = *m.get_one .expect; assert_eq!; assert!;const fn string() -> SelfStringparser for argument valuesSee also:
Example
# use clap_builder as clap; let mut cmd = new .arg; let m = cmd.try_get_matches_from_mut.unwrap; let port: &String = m.get_one .expect; assert_eq!;const fn os_string() -> Self[
OsString][std::ffi::OsString] parser for argument valuesExample
#const fn path_buf() -> Self[
PathBuf][std::path::PathBuf] parser for argument valuesExample
# use clap_builder as clap; # use PathBuf; # use Path; let mut cmd = new .arg; let m = cmd.try_get_matches_from_mut.unwrap; let port: &PathBuf = m.get_one .expect; assert_eq!; assert!;
impl ValueParser
fn type_id(&self) -> AnyValueIdDescribes the content of
AnyValuefn 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
impl Clone for ValueParser
fn clone(&self) -> Self
impl Debug for ValueParser
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl From<Range<i64>> for ValueParser
fn from(value: Range<i64>) -> Self
impl From<RangeFrom<i64>> for ValueParser
fn from(value: RangeFrom<i64>) -> Self
impl From<RangeFull> for ValueParser
fn from(value: RangeFull) -> Self
impl From<RangeInclusive<i64>> for ValueParser
fn from(value: RangeInclusive<i64>) -> Self
impl From<RangeTo<i64>> for ValueParser
fn from(value: RangeTo<i64>) -> Self
impl From<RangeToInclusive<i64>> for ValueParser
fn from(value: RangeToInclusive<i64>) -> Self
impl<P> From<P> for ValueParser
where
P: TypedValueParser + Send + Sync + 'static,
fn from(p: P) -> Self
impl<P> From<Vec<P>> for ValueParser
where
P: Into<PossibleValue>,
fn from(values: Vec<P>) -> Self
impl<P, const C: usize> From<[P; C]> for ValueParser
where
P: Into<PossibleValue>,
fn from(values: [P; C]) -> Self
Auto Trait Implementations
impl !RefUnwindSafe for ValueParser
impl !UnwindSafe for ValueParser
impl Freeze for ValueParser
impl Send for ValueParser
impl Sync for ValueParser
impl Unpin for ValueParser
impl UnsafeUnpin for ValueParser
Blanket Implementations
impl<I> IntoResettable<ValueParser> for ValueParser
where
I: Into<ValueParser>,
fn into_resettable(self) -> Resettable<ValueParser>
impl<T> Any for ValueParser
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for ValueParser
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for ValueParser
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for ValueParser
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for ValueParser
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for ValueParser
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for ValueParser
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for ValueParser
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for ValueParser
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>