Struct EnumValueParser

pub struct EnumValueParser<E: ValueEnum + Clone + Send + Sync + 'static>(/* private field */);

Parse an [ValueEnum][crate::ValueEnum] value.

See also:

Example

# use clap_builder as clap;
# use std::ffi::OsStr;
# use clap::ColorChoice;
# use clap::builder::TypedValueParser;
# let cmd = clap::Command::new("test");
# let arg = None;

// Usage
let mut cmd = clap::Command::new("raw")
    .arg(
        clap::Arg::new("color")
            .value_parser(clap::builder::EnumValueParser::<ColorChoice>::new())
            .required(true)
    );

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

// Semantics
let value_parser = clap::builder::EnumValueParser::<ColorChoice>::new();
// or
let value_parser = clap::value_parser!(ColorChoice);
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(), ColorChoice::Always);
assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("auto")).unwrap(), ColorChoice::Auto);
assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("never")).unwrap(), ColorChoice::Never);

Implementations

impl<E: ValueEnum + Clone + Send + Sync + 'static> EnumValueParser<E>

fn new() -> Self

Parse an [ValueEnum][crate::ValueEnum]

Trait Implementations

impl<E: Clone + ValueEnum + Clone + Send + Sync + 'static> Clone for EnumValueParser<E>

fn clone(&self) -> EnumValueParser<E>

impl<E: Debug + ValueEnum + Clone + Send + Sync + 'static> Debug for EnumValueParser<E>

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

impl<E: ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueParser<E>

fn default() -> Self

impl<E: ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for EnumValueParser<E>

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

Auto Trait Implementations

impl<E> Freeze for EnumValueParser<E> where PhantomData<E>: Freeze,

impl<E> RefUnwindSafe for EnumValueParser<E> where PhantomData<E>: RefUnwindSafe,

impl<E> Send for EnumValueParser<E> where PhantomData<E>: Send,

impl<E> Sync for EnumValueParser<E> where PhantomData<E>: Sync,

impl<E> Unpin for EnumValueParser<E> where PhantomData<E>: Unpin,

impl<E> UnsafeUnpin for EnumValueParser<E> where PhantomData<E>: UnsafeUnpin,

impl<E> UnwindSafe for EnumValueParser<E> where PhantomData<E>: UnwindSafe,

Blanket Implementations

impl<I> IntoResettable<ValueParser> for EnumValueParser<E> where I: Into<ValueParser>,

fn into_resettable(self) -> Resettable<ValueParser>

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for EnumValueParser<E> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for EnumValueParser<E> where T: ?Sized,

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

impl<T> CloneToUninit for EnumValueParser<E> where T: Clone,

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

impl<T> From<T> for EnumValueParser<E>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for EnumValueParser<E> where T: Clone,

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

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

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