clap/examples/escaped-positional.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

// Note: this requires the `cargo` feature
2022-05-23 10:50:11 -05:00
use clap::{arg, command, value_parser};
2017-05-31 14:41:54 -05:00
fn main() {
2022-02-15 08:33:38 -06:00
let matches = command!()
.arg(arg!(eff: -f))
2021-06-16 06:28:25 +01:00
.arg(
2022-05-23 10:50:11 -05:00
arg!(pea: -p <PEAR>)
.required(false)
.value_parser(value_parser!(String)),
)
.arg(
// Indicates that `slop` is only accessible after `--`.
arg!(slop: [SLOP])
.multiple_occurrences(true)
.last(true)
.value_parser(value_parser!(String)),
2021-06-16 06:28:25 +01:00
)
2017-05-31 14:41:54 -05:00
.get_matches();
// This is what will happen with `myprog -f -p=bob -- sloppy slop slop`...
2022-05-23 10:50:11 -05:00
// -f used: true
println!("-f used: {:?}", matches.is_present("eff"));
// -p's value: Some("bob")
println!("-p's value: {:?}", matches.get_one::<String>("pea"));
2022-05-23 10:50:11 -05:00
// 'slops' values: Some(["sloppy", "slop", "slop"])
2018-01-24 23:05:05 -05:00
println!(
"'slops' values: {:?}",
matches
2022-05-23 10:50:11 -05:00
.get_many::<String>("slop")
2018-01-24 23:05:05 -05:00
.map(|vals| vals.collect::<Vec<_>>())
.unwrap_or_default()
2022-05-23 10:50:11 -05:00
);
2017-05-31 14:41:54 -05:00
// Continued program logic goes here...
}