• 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<'_>) -> Command<'_>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     }
augment_args_for_update(cmd: Command<'_>) -> Command<'_>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 
main()78 fn main() {
79     let args = Cli::parse();
80     println!("{:#?}", args);
81 }
82