clap/examples/escaped-positional.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

// Note: this requires the `cargo` feature
2022-05-23 15:50:11 +00:00
use clap::{arg, command, value_parser};
2017-05-31 19:41:54 +00:00
fn main() {
2022-02-15 14:33:38 +00:00
let matches = command!()
.arg(arg!(eff: -f))
2021-06-16 05:28:25 +00:00
.arg(
2022-05-23 15:50:11 +00: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 05:28:25 +00:00
)
2017-05-31 19:41:54 +00:00
.get_matches();
// This is what will happen with `myprog -f -p=bob -- sloppy slop slop`...
2022-05-23 15:50:11 +00: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 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<_>>())
.unwrap_or_default()
2022-05-23 15:50:11 +00:00
);
2017-05-31 19:41:54 +00:00
// Continued program logic goes here...
}