clap/examples/tutorial_builder/04_01_enum.rs

33 lines
757 B
Rust
Raw Normal View History

// Note: this requires the `cargo` feature
2022-05-23 15:50:11 +00:00
use clap::{arg, command, value_parser, ArgEnum};
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum)]
enum Mode {
Fast,
Slow,
}
fn main() {
2022-02-15 14:33:38 +00:00
let matches = command!()
.arg(
arg!(<MODE>)
.help("What mode to run the program in")
2022-05-23 15:50:11 +00:00
.value_parser(value_parser!(Mode)),
)
.get_matches();
// Note, it's safe to call unwrap() because the arg is required
match matches
2022-05-23 15:50:11 +00:00
.get_one::<Mode>("MODE")
.expect("'MODE' is required and parsing will fail if its missing")
{
Mode::Fast => {
println!("Hare");
}
Mode::Slow => {
println!("Tortoise");
}
}
}