| 1 | use clap::{arg, command, value_parser, ArgAction}; |
| 2 | |
| 3 | fn main() { |
| 4 | let matches = command!() // requires `cargo` feature |
| 5 | .arg(arg!(eff: -f).action(ArgAction::SetTrue)) |
| 6 | .arg(arg!(pea: -p <PEAR>).value_parser(value_parser!(String))) |
| 7 | .arg( |
| 8 | // Indicates that `slop` is only accessible after `--`. |
| 9 | arg!(slop: [SLOP]) |
| 10 | .num_args(1..) |
| 11 | .last(true) |
| 12 | .value_parser(value_parser!(String)), |
| 13 | ) |
| 14 | .get_matches(); |
| 15 | |
| 16 | // This is what will happen with `myprog -f -p=bob -- sloppy slop slop`... |
| 17 | |
| 18 | // -f used: true |
| 19 | println!("-f used: {:?}" , matches.get_flag("eff" )); |
| 20 | // -p's value: Some("bob") |
| 21 | println!("-p's value: {:?}" , matches.get_one::<String>("pea" )); |
| 22 | // 'slops' values: Some(["sloppy", "slop", "slop"]) |
| 23 | println!( |
| 24 | "'slops' values: {:?}" , |
| 25 | matches |
| 26 | .get_many::<String>("slop" ) |
| 27 | .map(|vals| vals.collect::<Vec<_>>()) |
| 28 | .unwrap_or_default() |
| 29 | ); |
| 30 | |
| 31 | // Continued program logic goes here... |
| 32 | } |
| 33 | |