2022-07-19 18:29:31 +00:00
|
|
|
use clap::{arg, Args, Command, FromArgMatches as _};
|
2022-03-07 20:43:51 +00:00
|
|
|
|
2022-07-19 18:29:31 +00:00
|
|
|
#[derive(Args, Debug)]
|
2022-03-07 20:43:51 +00:00
|
|
|
struct DerivedArgs {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long)]
|
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-09-02 00:40:56 +00:00
|
|
|
println!("Value of built: {:?}", matches.get_flag("built"));
|
2022-03-07 20:43:51 +00:00
|
|
|
println!(
|
|
|
|
"Value of derived via ArgMatches: {:?}",
|
2022-09-02 00:40:56 +00:00
|
|
|
matches.get_flag("derived")
|
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);
|
|
|
|
}
|