• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use clap::{arg, Args, Command, FromArgMatches as _};
2 
3 #[derive(Args, Debug)]
4 struct DerivedArgs {
5     #[arg(short, long)]
6     derived: bool,
7 }
8 
main()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!("Value of built: {:?}", matches.get_flag("built"));
16     println!(
17         "Value of derived via ArgMatches: {:?}",
18         matches.get_flag("derived")
19     );
20 
21     // Since DerivedArgs implements FromArgMatches, we can extract it from the unstructured ArgMatches.
22     // This is the main benefit of using derived arguments.
23     let derived_matches = DerivedArgs::from_arg_matches(&matches)
24         .map_err(|err| err.exit())
25         .unwrap();
26     println!("Value of derived: {:#?}", derived_matches);
27 }
28