mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
647896d929
This is the derive support for #3774 (see also #3775, #3777) This combined with `value_parser` replaces `parser`. The main frustration with this is that `ArgAction::Count` (the replacement for `parse(from_occurrences)` must be a `u64`. We could come up with a magic attribute that is meant to be the value parser's parsed type. We could then use `TryFrom` to convert the parsed type to the user's type to allow more. That is an exercise for the future. Alternatively, we have #3792. Prep for this included - #3782 - #3783 - #3786 - #3789 - #3793
81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
use clap::error::{Error, ErrorKind};
|
|
use clap::{ArgMatches, Args as _, Command, FromArgMatches, Parser, Subcommand};
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct AddArgs {
|
|
#[clap(value_parser)]
|
|
name: Vec<String>,
|
|
}
|
|
#[derive(Parser, Debug)]
|
|
struct RemoveArgs {
|
|
#[clap(short, long, action)]
|
|
force: bool,
|
|
#[clap(value_parser)]
|
|
name: Vec<String>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum CliSub {
|
|
Add(AddArgs),
|
|
Remove(RemoveArgs),
|
|
}
|
|
|
|
impl FromArgMatches for CliSub {
|
|
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
|
|
match matches.subcommand() {
|
|
Some(("add", args)) => Ok(Self::Add(AddArgs::from_arg_matches(args)?)),
|
|
Some(("remove", args)) => Ok(Self::Remove(RemoveArgs::from_arg_matches(args)?)),
|
|
Some((_, _)) => Err(Error::raw(
|
|
ErrorKind::UnrecognizedSubcommand,
|
|
"Valid subcommands are `add` and `remove`",
|
|
)),
|
|
None => Err(Error::raw(
|
|
ErrorKind::MissingSubcommand,
|
|
"Valid subcommands are `add` and `remove`",
|
|
)),
|
|
}
|
|
}
|
|
fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
|
|
match matches.subcommand() {
|
|
Some(("add", args)) => *self = Self::Add(AddArgs::from_arg_matches(args)?),
|
|
Some(("remove", args)) => *self = Self::Remove(RemoveArgs::from_arg_matches(args)?),
|
|
Some((_, _)) => {
|
|
return Err(Error::raw(
|
|
ErrorKind::UnrecognizedSubcommand,
|
|
"Valid subcommands are `add` and `remove`",
|
|
))
|
|
}
|
|
None => (),
|
|
};
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Subcommand for CliSub {
|
|
fn augment_subcommands(cmd: Command<'_>) -> Command<'_> {
|
|
cmd.subcommand(AddArgs::augment_args(Command::new("add")))
|
|
.subcommand(RemoveArgs::augment_args(Command::new("remove")))
|
|
.subcommand_required(true)
|
|
}
|
|
fn augment_subcommands_for_update(cmd: Command<'_>) -> Command<'_> {
|
|
cmd.subcommand(AddArgs::augment_args(Command::new("add")))
|
|
.subcommand(RemoveArgs::augment_args(Command::new("remove")))
|
|
.subcommand_required(true)
|
|
}
|
|
fn has_subcommand(name: &str) -> bool {
|
|
matches!(name, "add" | "remove")
|
|
}
|
|
}
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct Cli {
|
|
#[clap(short, long, action)]
|
|
top_level: bool,
|
|
#[clap(subcommand)]
|
|
subcommand: CliSub,
|
|
}
|
|
|
|
fn main() {
|
|
let args = Cli::parse();
|
|
println!("{:#?}", args);
|
|
}
|