2015-03-29 01:19:51 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// If you have arguments of specific values you want to test for, you can use the
|
|
|
|
// .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"
|
2015-04-29 17:32:00 +00:00
|
|
|
let mode_vals = ["fast", "slow"];
|
2015-03-29 01:19:51 +00:00
|
|
|
let matches = App::new("myapp").about("does awesome things")
|
2015-04-14 02:18:50 +00:00
|
|
|
.arg(Arg::with_name("MODE")
|
2015-03-29 01:19:51 +00:00
|
|
|
.help("What mode to run the program in")
|
|
|
|
.index(1)
|
2015-04-29 17:32:00 +00:00
|
|
|
.possible_values(&mode_vals)
|
2015-03-29 01:19:51 +00:00
|
|
|
.required(true))
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
// Note, it's safe to call unwrap() because the arg is required
|
|
|
|
match matches.value_of("MODE").unwrap() {
|
|
|
|
"fast" => {
|
|
|
|
// Do fast things...
|
|
|
|
},
|
|
|
|
"slow" => {
|
|
|
|
// Do slow things...
|
|
|
|
},
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|