]> git.proxmox.com Git - rustc.git/blob - vendor/clap/examples/derive_ref/flatten_hand_args.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / clap / examples / derive_ref / flatten_hand_args.rs
1 use clap::error::Error;
2 use clap::{Arg, ArgAction, ArgMatches, Args, Command, FromArgMatches, Parser};
3
4 #[derive(Debug)]
5 struct CliArgs {
6 foo: bool,
7 bar: bool,
8 quuz: Option<String>,
9 }
10
11 impl FromArgMatches for CliArgs {
12 fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
13 let mut matches = matches.clone();
14 Self::from_arg_matches_mut(&mut matches)
15 }
16 fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
17 Ok(Self {
18 foo: *matches.get_one::<bool>("foo").expect("defaulted by clap"),
19 bar: *matches.get_one::<bool>("bar").expect("defaulted by clap"),
20 quuz: matches.remove_one::<String>("quuz"),
21 })
22 }
23 fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
24 let mut matches = matches.clone();
25 self.update_from_arg_matches_mut(&mut matches)
26 }
27 fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
28 self.foo |= *matches.get_one::<bool>("foo").expect("defaulted by clap");
29 self.bar |= *matches.get_one::<bool>("bar").expect("defaulted by clap");
30 if let Some(quuz) = matches.remove_one::<String>("quuz") {
31 self.quuz = Some(quuz);
32 }
33 Ok(())
34 }
35 }
36
37 impl Args for CliArgs {
38 fn augment_args(cmd: Command<'_>) -> Command<'_> {
39 cmd.arg(
40 Arg::new("foo")
41 .short('f')
42 .long("foo")
43 .action(ArgAction::SetTrue),
44 )
45 .arg(
46 Arg::new("bar")
47 .short('b')
48 .long("bar")
49 .action(ArgAction::SetTrue),
50 )
51 .arg(Arg::new("quuz").short('q').long("quuz").takes_value(true))
52 }
53 fn augment_args_for_update(cmd: Command<'_>) -> Command<'_> {
54 cmd.arg(
55 Arg::new("foo")
56 .short('f')
57 .long("foo")
58 .action(ArgAction::SetTrue),
59 )
60 .arg(
61 Arg::new("bar")
62 .short('b')
63 .long("bar")
64 .action(ArgAction::SetTrue),
65 )
66 .arg(Arg::new("quuz").short('q').long("quuz").takes_value(true))
67 }
68 }
69
70 #[derive(Parser, Debug)]
71 struct Cli {
72 #[clap(short, long, action)]
73 top_level: bool,
74 #[clap(flatten)]
75 more_args: CliArgs,
76 }
77
78 fn main() {
79 let args = Cli::parse();
80 println!("{:#?}", args);
81 }