mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
0cc2f69839
Breaking Change Instead of requiring a Vec<&str> for various Arg::*_all() and Arg::possible_values() methods this commit now requires a generic IntoIterator<Item=AsRef<str>> which allows things such as constant arrays. This change requires that any Arg::*_all() methods be changed from vec!["val", "val"] -> let vals = ["val", "val"]; some_arg.possible_values(&vals) (or vals.iter()). Closes #87
34 lines
No EOL
1.3 KiB
Rust
34 lines
No EOL
1.3 KiB
Rust
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"
|
|
let mode_vals = ["fast", "slow"];
|
|
let matches = App::new("myapp").about("does awesome things")
|
|
.arg(Arg::with_name("MODE")
|
|
.help("What mode to run the program in")
|
|
.index(1)
|
|
.possible_values(&mode_vals)
|
|
.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!()
|
|
}
|
|
} |