summaryrefslogtreecommitdiffstats
path: root/vendor/clap/examples/derive_ref/augment_args.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/clap/examples/derive_ref/augment_args.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap/examples/derive_ref/augment_args.rs')
-rw-r--r--vendor/clap/examples/derive_ref/augment_args.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/vendor/clap/examples/derive_ref/augment_args.rs b/vendor/clap/examples/derive_ref/augment_args.rs
new file mode 100644
index 000000000..390c72f4a
--- /dev/null
+++ b/vendor/clap/examples/derive_ref/augment_args.rs
@@ -0,0 +1,30 @@
+use clap::{arg, Args as _, Command, FromArgMatches as _, Parser};
+
+#[derive(Parser, Debug)]
+struct DerivedArgs {
+ #[clap(short, long, action)]
+ derived: bool,
+}
+
+fn main() {
+ let cli = Command::new("CLI").arg(arg!(-b - -built).action(clap::ArgAction::SetTrue));
+ // Augment built args with derived args
+ let cli = DerivedArgs::augment_args(cli);
+
+ let matches = cli.get_matches();
+ println!(
+ "Value of built: {:?}",
+ *matches.get_one::<bool>("built").unwrap()
+ );
+ println!(
+ "Value of derived via ArgMatches: {:?}",
+ *matches.get_one::<bool>("derived").unwrap()
+ );
+
+ // 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);
+}