Skip to content

Commit

Permalink
fix(derive): Allow other attributes with subcommand that has subcommands
Browse files Browse the repository at this point in the history
This was overlooked when we added support for `#[clap(subcommand)]` to
variants.

Fixes #3504
  • Loading branch information
epage committed Feb 23, 2022
1 parent 15f43f8 commit cb93764
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
8 changes: 1 addition & 7 deletions clap_derive/src/attrs.rs
Expand Up @@ -127,15 +127,9 @@ impl Attrs {
"parse attribute is not allowed for subcommand"
);
}
if res.has_explicit_methods() {
abort!(
res.kind.span(),
"methods in attributes are not allowed for subcommand"
);
}

use syn::Fields::*;
use syn::FieldsUnnamed;

let field_ty = match variant.fields {
Named(_) => {
abort!(variant.span(), "structs are not allowed for subcommand");
Expand Down
13 changes: 12 additions & 1 deletion tests/derive/subcommands.rs
Expand Up @@ -296,7 +296,9 @@ fn external_subcommand_optional() {
fn enum_in_enum_subsubcommand() {
#[derive(Parser, Debug, PartialEq)]
pub enum Opt {
#[clap(subcommand)]
#[clap(alias = "l")]
List,
#[clap(subcommand, alias = "d")]
Daemon(DaemonCommand),
}

Expand All @@ -309,11 +311,20 @@ fn enum_in_enum_subsubcommand() {
let result = Opt::try_parse_from(&["test"]);
assert!(result.is_err());

let result = Opt::try_parse_from(&["test", "list"]).unwrap();
assert_eq!(Opt::List, result);

let result = Opt::try_parse_from(&["test", "l"]).unwrap();
assert_eq!(Opt::List, result);

let result = Opt::try_parse_from(&["test", "daemon"]);
assert!(result.is_err());

let result = Opt::try_parse_from(&["test", "daemon", "start"]).unwrap();
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);

let result = Opt::try_parse_from(&["test", "d", "start"]).unwrap();
assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
}

#[test]
Expand Down

0 comments on commit cb93764

Please sign in to comment.