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-06-10 01:03:28 +00:00
|
|
|
foo: *matches.get_one::<bool>("foo").expect("defaulted by clap"),
|
|
|
|
bar: *matches.get_one::<bool>("bar").expect("defaulted by clap"),
|
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-06-10 01:03:28 +00:00
|
|
|
self.foo |= *matches.get_one::<bool>("foo").expect("defaulted by clap");
|
|
|
|
self.bar |= *matches.get_one::<bool>("bar").expect("defaulted by clap");
|
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 {
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("quuz").short('q').long("quuz").takes_value(true))
|
2022-03-07 20:43:51 +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),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("quuz").short('q').long("quuz").takes_value(true))
|
2022-03-07 20:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
struct Cli {
|
2022-06-10 01:03:28 +00:00
|
|
|
#[clap(short, long, action)]
|
2022-03-07 20:43:51 +00:00
|
|
|
top_level: bool,
|
|
|
|
#[clap(flatten)]
|
|
|
|
more_args: CliArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = Cli::parse();
|
|
|
|
println!("{:#?}", args);
|
|
|
|
}
|