2015-03-29 01:19:51 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
fn main() {
|
2015-10-01 01:45:35 +00:00
|
|
|
// If you have arguments of specific values you want to test for, you can use the
|
2015-03-29 01:19:51 +00:00
|
|
|
// .possible_values() method of Arg
|
|
|
|
//
|
|
|
|
// This allows you specify the valid values for that argument. If the user does not use one of
|
|
|
|
// those specific values, they will receive a graceful exit with error message informing them
|
|
|
|
// of the mistake, and what the possible valid values are
|
|
|
|
//
|
|
|
|
// For this example, assume you want one positional argument of either "fast" or "slow"
|
|
|
|
// i.e. the only possible ways to run the program are "myprog fast" or "myprog slow"
|
2018-01-25 04:05:05 +00:00
|
|
|
let matches = App::new("myapp")
|
|
|
|
.about("does awesome things")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("MODE")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("What mode to run the program in")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1)
|
|
|
|
.possible_values(&["fast", "slow"])
|
|
|
|
.required(true),
|
|
|
|
)
|
|
|
|
.get_matches();
|
2015-03-29 01:19:51 +00:00
|
|
|
|
|
|
|
// Note, it's safe to call unwrap() because the arg is required
|
|
|
|
match matches.value_of("MODE").unwrap() {
|
|
|
|
"fast" => {
|
|
|
|
// Do fast things...
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2015-03-29 01:19:51 +00:00
|
|
|
"slow" => {
|
|
|
|
// Do slow things...
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2015-03-29 01:19:51 +00:00
|
|
|
}
|
2016-01-27 22:22:34 +00:00
|
|
|
}
|