mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
5580e8c465
* feat(arg_value): ArgValue can be used for possible_values Through the ArgValue it is possible: * `hide` possible_values from showing in completion, help and validation * add `about` to possible_values in completion * Resolved a few change-requests by epage * make clippy happy * add ArgValue::get_visible_value * remove verbose destructering * rename ArgValue::get_hidden to ArgValue::is_hidden * add test for help output of hidden ArgValues * Documentation for ArgValue There is an issue that required to implement From<&ArgValue> for ArgValue. We should probably find a solution without that. * fix requested changes by epage * fix formatting * add deref in possible_values call to remove From<&&str> * make clippy happy * use copied() instad of map(|v|*v) * Finishing up for merge, hopefully * changes requested by pksunkara
34 lines
1.2 KiB
Rust
34 lines
1.2 KiB
Rust
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 matches = App::new("myapp")
|
|
.about("does awesome things")
|
|
.arg(
|
|
Arg::new("MODE")
|
|
.about("What mode to run the program in")
|
|
.index(1)
|
|
.possible_values(["fast", "slow"])
|
|
.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!(),
|
|
}
|
|
}
|