Struct PossibleValuesParser

pub struct PossibleValuesParser(/* private field */);

Verify the value is from an enumerated set of [PossibleValue][crate::builder::PossibleValue].

See also:

Example

Usage:

# use clap_builder as clap;
let mut cmd = clap::Command::new("raw")
    .arg(
        clap::Arg::new("color")
            .value_parser(clap::builder::PossibleValuesParser::new(["always", "auto", "never"]))
            .required(true)
    );

let m = cmd.try_get_matches_from_mut(["cmd", "always"]).unwrap();
let port: &String = m.get_one("color")
    .expect("required");
assert_eq!(port, "always");

Semantics:

# use clap_builder as clap;
# use std::ffi::OsStr;
# use clap::builder::TypedValueParser;
# let cmd = clap::Command::new("test");
# let arg = None;
let value_parser = clap::builder::PossibleValuesParser::new(["always", "auto", "never"]);
assert!(value_parser.parse_ref(&cmd, arg, OsStr::new("random")).is_err());
assert!(value_parser.parse_ref(&cmd, arg, OsStr::new("")).is_err());
assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("always")).unwrap(), "always");
assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("auto")).unwrap(), "auto");
assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("never")).unwrap(), "never");

Implementations

impl PossibleValuesParser

fn new(values: impl Into<PossibleValuesParser>) -> Self

Verify the value is from an enumerated set of [PossibleValue][crate::builder::PossibleValue].

Trait Implementations

impl Clone for PossibleValuesParser

fn clone(&self) -> PossibleValuesParser

impl Debug for PossibleValuesParser

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl TypedValueParser for PossibleValuesParser

type Value = String;
fn parse_ref(&self, cmd: &Command, arg: Option<&Arg>, value: &OsStr) -> Result<Self::Value, Error>
fn parse(&self, cmd: &Command, arg: Option<&Arg>, value: OsString) -> Result<String, Error>
fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>>

impl<I, T> From<I> for PossibleValuesParser where I: IntoIterator<Item = T>, T: Into<PossibleValue>,

fn from(values: I) -> Self

Auto Trait Implementations

impl Freeze for PossibleValuesParser

impl RefUnwindSafe for PossibleValuesParser

impl Send for PossibleValuesParser

impl Sync for PossibleValuesParser

impl Unpin for PossibleValuesParser

impl UnsafeUnpin for PossibleValuesParser

impl UnwindSafe for PossibleValuesParser

Blanket Implementations

impl<I> IntoResettable<ValueParser> for PossibleValuesParser where I: Into<ValueParser>,

fn into_resettable(self) -> Resettable<ValueParser>

impl<T> Any for PossibleValuesParser where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for PossibleValuesParser where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for PossibleValuesParser where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for PossibleValuesParser where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for PossibleValuesParser

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for PossibleValuesParser where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for PossibleValuesParser where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for PossibleValuesParser 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 PossibleValuesParser where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>