2022-03-07 20:43:51 +00:00
|
|
|
use clap::error::Error;
|
2022-06-10 01:03:28 +00:00
|
|
|
use clap::{Arg, ArgAction, ArgMatches, Args, Command, FromArgMatches, Parser};
|
2022-03-07 20:43:51 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CliArgs {
|
|
|
|
foo: bool,
|
|
|
|
bar: bool,
|
|
|
|
quuz: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromArgMatches for CliArgs {
|
|
|
|
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
|
2022-05-23 17:44:21 +00:00
|
|
|
let mut matches = matches.clone();
|
|
|
|
Self::from_arg_matches_mut(&mut matches)
|
|
|
|
}
|
|
|
|
fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
|
2022-03-07 20:43:51 +00:00
|
|
|
Ok(Self {
|
2022-09-02 00:40:56 +00:00
|
|
|
foo: matches.get_flag("foo"),
|
|
|
|
bar: matches.get_flag("bar"),
|
2022-05-25 15:46:42 +00:00
|
|
|
quuz: matches.remove_one::<String>("quuz"),
|
2022-03-07 20:43:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
|
2022-05-23 17:44:21 +00:00
|
|
|
let mut matches = matches.clone();
|
|
|
|
self.update_from_arg_matches_mut(&mut matches)
|
|
|
|
}
|
|
|
|
fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
|
2022-09-02 00:40:56 +00:00
|
|
|
self.foo |= matches.get_flag("foo");
|
|
|
|
self.bar |= matches.get_flag("bar");
|
2022-05-25 15:46:42 +00:00
|
|
|
if let Some(quuz) = matches.remove_one::<String>("quuz") {
|
2022-05-24 21:20:12 +00:00
|
|
|
self.quuz = Some(quuz);
|
2022-03-07 20:43:51 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Args for CliArgs {
|
2022-08-15 19:29:46 +00:00
|
|
|
fn augment_args(cmd: Command) -> Command {
|
2022-06-10 01:03:28 +00:00
|
|
|
cmd.arg(
|
|
|
|
Arg::new("foo")
|
|
|
|
.short('f')
|
|
|
|
.long("foo")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("bar")
|
|
|
|
.short('b')
|
|
|
|
.long("bar")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("quuz")
|
|
|
|
.short('q')
|
|
|
|
.long("quuz")
|
|
|
|
.action(ArgAction::Set),
|
|
|
|
)
|
2022-03-07 20:43:51 +00:00
|
|
|
}
|
2022-08-15 19:29:46 +00:00
|
|
|
fn augment_args_for_update(cmd: Command) -> Command {
|
2022-06-10 01:03:28 +00:00
|
|
|
cmd.arg(
|
|
|
|
Arg::new("foo")
|
|
|
|
.short('f')
|
|
|
|
.long("foo")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("bar")
|
|
|
|
.short('b')
|
|
|
|
.long("bar")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("quuz")
|
|
|
|
.short('q')
|
|
|
|
.long("quuz")
|
|
|
|
.action(ArgAction::Set),
|
|
|
|
)
|
2022-03-07 20:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
struct Cli {
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long)]
|
2022-03-07 20:43:51 +00:00
|
|
|
top_level: bool,
|
|
|
|
#[clap(flatten)]
|
|
|
|
more_args: CliArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = Cli::parse();
|
|
|
|
println!("{:#?}", args);
|
|
|
|
}
|