|
1 | | -use clap::{arg, command, value_parser, ValueEnum}; |
| 1 | +use clap::{arg, builder::PossibleValue, command, value_parser, ValueEnum}; |
2 | 2 |
|
3 | | -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] // requires `derive` feature |
| 3 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
4 | 4 | enum Mode { |
5 | 5 | Fast, |
6 | 6 | Slow, |
7 | 7 | } |
8 | 8 |
|
| 9 | +// Can also be derived] with feature flag `derive` |
| 10 | +impl ValueEnum for Mode { |
| 11 | + fn value_variants<'a>() -> &'a [Self] { |
| 12 | + &[Mode::Fast, Mode::Slow] |
| 13 | + } |
| 14 | + |
| 15 | + fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> { |
| 16 | + Some(match self { |
| 17 | + Mode::Fast => PossibleValue::new("fast"), |
| 18 | + Mode::Slow => PossibleValue::new("slow"), |
| 19 | + }) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl std::fmt::Display for Mode { |
| 24 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | + self.to_possible_value() |
| 26 | + .expect("no values are skipped") |
| 27 | + .get_name() |
| 28 | + .fmt(f) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl std::str::FromStr for Mode { |
| 33 | + type Err = String; |
| 34 | + |
| 35 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 36 | + for variant in Self::value_variants() { |
| 37 | + if variant.to_possible_value().unwrap().matches(s, false) { |
| 38 | + return Ok(*variant); |
| 39 | + } |
| 40 | + } |
| 41 | + Err(format!("Invalid variant: {}", s)) |
| 42 | + } |
| 43 | +} |
| 44 | + |
9 | 45 | fn main() { |
10 | 46 | let matches = command!() // requires `cargo` feature |
11 | 47 | .arg( |
|
0 commit comments