Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(derive): Allow other attributes with subcommand that has subcommands #3505

Merged
merged 2 commits into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -125,7 +125,7 @@ jobs:
override: true
- uses: Swatinem/rust-cache@v1
- name: UI Tests
run: cargo test --test derive_ui --features derive
run: make test-ui
docs:
name: Docs
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Expand Up @@ -10,6 +10,8 @@ ifneq (${TOOLCHAIN_TARGET},)
ARGS+=--target ${TOOLCHAIN_TARGET}
endif

MSRV?=1.54.0

_FEATURES = minimal default wasm full debug release
_FEATURES_minimal = --no-default-features --features "std"
_FEATURES_default =
Expand All @@ -29,3 +31,6 @@ test-%:

clippy-%:
cargo clippy ${_FEATURES_${@:clippy-%=%}} ${ARGS} --all-targets -- -D warnings -A deprecated

test-ui:
cargo +${MSRV} test --test derive_ui --features derive
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