2022-03-07 20:43:51 +00:00
|
|
|
use clap::{arg, Args as _, Command, FromArgMatches as _, Parser};
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
struct DerivedArgs {
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(short, long, action)]
|
2022-03-07 20:43:51 +00:00
|
|
|
derived: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-06-01 15:31:23 +00:00
|
|
|
let cli = Command::new("CLI").arg(arg!(-b - -built).action(clap::ArgAction::SetTrue));
|
2022-03-07 20:43:51 +00:00
|
|
|
// Augment built args with derived args
|
|
|
|
let cli = DerivedArgs::augment_args(cli);
|
|
|
|
|
|
|
|
let matches = cli.get_matches();
|
2022-06-01 15:31:23 +00:00
|
|
|
println!(
|
|
|
|
"Value of built: {:?}",
|
|
|
|
*matches.get_one::<bool>("built").unwrap()
|
|
|
|
);
|
2022-03-07 20:43:51 +00:00
|
|
|
println!(
|
|
|
|
"Value of derived via ArgMatches: {:?}",
|
2022-06-01 15:31:23 +00:00
|
|
|
*matches.get_one::<bool>("derived").unwrap()
|
2022-03-07 20:43:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Since DerivedArgs implements FromArgMatches, we can extract it from the unstructured ArgMatches.
|
|
|
|
// This is the main benefit of using derived arguments.
|
|
|
|
let derived_matches = DerivedArgs::from_arg_matches(&matches)
|
|
|
|
.map_err(|err| err.exit())
|
|
|
|
.unwrap();
|
|
|
|
println!("Value of derived: {:#?}", derived_matches);
|
|
|
|
}
|