Trait clap::ValueEnum

source ·
pub trait ValueEnum: Sized + Clone {
    fn value_variants<'a>() -> &'a [Self] ;
    fn to_possible_value(&self) -> Option<PossibleValue>;

    fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> { ... }
}
Expand description

Parse arguments into enums.

When deriving Parser, a field whose type implements ValueEnum can have the attribute #[arg(value_enum)] which will

  • Call EnumValueParser
  • Allowing using the #[arg(default_value_t)] attribute without implementing Display.

See the derive reference for attributes and best practices.

NOTE: Deriving requires the [derive feature flag][crate::_features]

Example

#[derive(clap::Parser)]
struct Args {
   #[arg(value_enum)]
   level: Level,
}

#[derive(clap::ValueEnum, Clone)]
enum Level {
   Debug,
   Info,
   Warning,
   Error,
}

Required Methods§

All possible argument values, in display order.

The canonical argument value.

The value is None for skipped variants.

Provided Methods§

Parse an argument into Self.

Implementors§