]>
Commit | Line | Data |
---|---|---|
04454e1e FG |
1 | // Note: this requires the `cargo` feature |
2 | ||
923072b8 | 3 | use clap::{arg, command, value_parser, ArgAction}; |
04454e1e FG |
4 | |
5 | fn main() { | |
6 | let matches = command!() | |
923072b8 | 7 | .arg(arg!(eff: -f).action(ArgAction::SetTrue)) |
04454e1e | 8 | .arg( |
923072b8 FG |
9 | arg!(pea: -p <PEAR>) |
10 | .required(false) | |
11 | .value_parser(value_parser!(String)), | |
12 | ) | |
13 | .arg( | |
14 | // Indicates that `slop` is only accessible after `--`. | |
15 | arg!(slop: [SLOP]) | |
16 | .multiple_values(true) | |
17 | .last(true) | |
18 | .value_parser(value_parser!(String)), | |
04454e1e FG |
19 | ) |
20 | .get_matches(); | |
21 | ||
22 | // This is what will happen with `myprog -f -p=bob -- sloppy slop slop`... | |
923072b8 FG |
23 | |
24 | // -f used: true | |
25 | println!( | |
26 | "-f used: {:?}", | |
27 | *matches.get_one::<bool>("eff").expect("defaulted by clap") | |
28 | ); | |
29 | // -p's value: Some("bob") | |
30 | println!("-p's value: {:?}", matches.get_one::<String>("pea")); | |
31 | // 'slops' values: Some(["sloppy", "slop", "slop"]) | |
04454e1e FG |
32 | println!( |
33 | "'slops' values: {:?}", | |
34 | matches | |
923072b8 | 35 | .get_many::<String>("slop") |
04454e1e FG |
36 | .map(|vals| vals.collect::<Vec<_>>()) |
37 | .unwrap_or_default() | |
923072b8 | 38 | ); |
04454e1e FG |
39 | |
40 | // Continued program logic goes here... | |
41 | } |