• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
from_arg_matches(matches: &ArgMatches) -> Result<Self, Error>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     }
from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error>16     fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
17         Ok(Self {
18             foo: matches.get_flag("foo"),
19             bar: matches.get_flag("bar"),
20             quuz: matches.remove_one::<String>("quuz"),
21         })
22     }
update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error>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     }
update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error>27     fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
28         self.foo |= matches.get_flag("foo");
29         self.bar |= matches.get_flag("bar");
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 {
augment_args(cmd: Command) -> Command38     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(
52             Arg::new("quuz")
53                 .short('q')
54                 .long("quuz")
55                 .action(ArgAction::Set),
56         )
57     }
augment_args_for_update(cmd: Command) -> Command58     fn augment_args_for_update(cmd: Command) -> Command {
59         cmd.arg(
60             Arg::new("foo")
61                 .short('f')
62                 .long("foo")
63                 .action(ArgAction::SetTrue),
64         )
65         .arg(
66             Arg::new("bar")
67                 .short('b')
68                 .long("bar")
69                 .action(ArgAction::SetTrue),
70         )
71         .arg(
72             Arg::new("quuz")
73                 .short('q')
74                 .long("quuz")
75                 .action(ArgAction::Set),
76         )
77     }
78 }
79 
80 #[derive(Parser, Debug)]
81 struct Cli {
82     #[arg(short, long)]
83     top_level: bool,
84     #[command(flatten)]
85     more_args: CliArgs,
86 }
87 
main()88 fn main() {
89     let args = Cli::parse();
90     println!("{:#?}", args);
91 }
92