2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, command, value_parser, ArgAction};
|
2017-05-31 19:41:54 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-19 18:29:31 +00:00
|
|
|
let matches = command!() // requires `cargo` feature
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(eff: -f).action(ArgAction::SetTrue))
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(pea: -p <PEAR>).value_parser(value_parser!(String)))
|
2022-05-23 15:50:11 +00:00
|
|
|
.arg(
|
|
|
|
// Indicates that `slop` is only accessible after `--`.
|
|
|
|
arg!(slop: [SLOP])
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-05-23 15:50:11 +00:00
|
|
|
.last(true)
|
|
|
|
.value_parser(value_parser!(String)),
|
2021-06-16 05:28:25 +00:00
|
|
|
)
|
2017-05-31 19:41:54 +00:00
|
|
|
.get_matches();
|
|
|
|
|
2021-08-03 11:41:54 +00:00
|
|
|
// This is what will happen with `myprog -f -p=bob -- sloppy slop slop`...
|
2022-05-23 15:50:11 +00:00
|
|
|
|
|
|
|
// -f used: true
|
2022-09-02 00:40:56 +00:00
|
|
|
println!("-f used: {:?}", matches.get_flag("eff"));
|
2022-05-23 15:50:11 +00:00
|
|
|
// -p's value: Some("bob")
|
2022-05-25 15:46:42 +00:00
|
|
|
println!("-p's value: {:?}", matches.get_one::<String>("pea"));
|
2022-05-23 15:50:11 +00:00
|
|
|
// 'slops' values: Some(["sloppy", "slop", "slop"])
|
2018-01-25 04:05:05 +00:00
|
|
|
println!(
|
|
|
|
"'slops' values: {:?}",
|
|
|
|
matches
|
2022-05-23 15:50:11 +00:00
|
|
|
.get_many::<String>("slop")
|
2018-01-25 04:05:05 +00:00
|
|
|
.map(|vals| vals.collect::<Vec<_>>())
|
2021-11-30 18:30:19 +00:00
|
|
|
.unwrap_or_default()
|
2022-05-23 15:50:11 +00:00
|
|
|
);
|
2017-05-31 19:41:54 +00:00
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|