]> git.proxmox.com Git - rustc.git/blob - vendor/clap/examples/derive_ref/augment_args.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / clap / examples / derive_ref / augment_args.rs
1 use clap::{arg, Args as _, Command, FromArgMatches as _, Parser};
2
3 #[derive(Parser, Debug)]
4 struct DerivedArgs {
5 #[clap(short, long, action)]
6 derived: bool,
7 }
8
9 fn main() {
10 let cli = Command::new("CLI").arg(arg!(-b - -built).action(clap::ArgAction::SetTrue));
11 // Augment built args with derived args
12 let cli = DerivedArgs::augment_args(cli);
13
14 let matches = cli.get_matches();
15 println!(
16 "Value of built: {:?}",
17 *matches.get_one::<bool>("built").unwrap()
18 );
19 println!(
20 "Value of derived via ArgMatches: {:?}",
21 *matches.get_one::<bool>("derived").unwrap()
22 );
23
24 // Since DerivedArgs implements FromArgMatches, we can extract it from the unstructured ArgMatches.
25 // This is the main benefit of using derived arguments.
26 let derived_matches = DerivedArgs::from_arg_matches(&matches)
27 .map_err(|err| err.exit())
28 .unwrap();
29 println!("Value of derived: {:#?}", derived_matches);
30 }