1 use clap
::error
::{Error, ErrorKind}
;
2 use clap
::{ArgMatches, Args as _, Command, FromArgMatches, Parser, Subcommand}
;
4 #[derive(Parser, Debug)]
9 #[derive(Parser, Debug)]
11 #[clap(short, long, action)]
23 impl FromArgMatches
for CliSub
{
24 fn from_arg_matches(matches
: &ArgMatches
) -> Result
<Self, Error
> {
25 match matches
.subcommand() {
26 Some(("add", args
)) => Ok(Self::Add(AddArgs
::from_arg_matches(args
)?
)),
27 Some(("remove", args
)) => Ok(Self::Remove(RemoveArgs
::from_arg_matches(args
)?
)),
28 Some((_
, _
)) => Err(Error
::raw(
29 ErrorKind
::UnrecognizedSubcommand
,
30 "Valid subcommands are `add` and `remove`",
32 None
=> Err(Error
::raw(
33 ErrorKind
::MissingSubcommand
,
34 "Valid subcommands are `add` and `remove`",
38 fn update_from_arg_matches(&mut self, matches
: &ArgMatches
) -> Result
<(), Error
> {
39 match matches
.subcommand() {
40 Some(("add", args
)) => *self = Self::Add(AddArgs
::from_arg_matches(args
)?
),
41 Some(("remove", args
)) => *self = Self::Remove(RemoveArgs
::from_arg_matches(args
)?
),
43 return Err(Error
::raw(
44 ErrorKind
::UnrecognizedSubcommand
,
45 "Valid subcommands are `add` and `remove`",
54 impl Subcommand
for CliSub
{
55 fn augment_subcommands(cmd
: Command
<'_
>) -> Command
<'_
> {
56 cmd
.subcommand(AddArgs
::augment_args(Command
::new("add")))
57 .subcommand(RemoveArgs
::augment_args(Command
::new("remove")))
58 .subcommand_required(true)
60 fn augment_subcommands_for_update(cmd
: Command
<'_
>) -> Command
<'_
> {
61 cmd
.subcommand(AddArgs
::augment_args(Command
::new("add")))
62 .subcommand(RemoveArgs
::augment_args(Command
::new("remove")))
63 .subcommand_required(true)
65 fn has_subcommand(name
: &str) -> bool
{
66 matches
!(name
, "add" | "remove")
70 #[derive(Parser, Debug)]
72 #[clap(short, long, action)]
79 let args
= Cli
::parse();
80 println
!("{:#?}", args
);