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

feat(assert): Help people know about implicit ArgGroups #4307

Merged
merged 1 commit into from Sep 30, 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
12 changes: 10 additions & 2 deletions src/builder/debug_asserts.rs
Expand Up @@ -280,20 +280,28 @@ pub(crate) fn assert_app(cmd: &Command) {
}

for group in cmd.get_groups() {
let derive_hint = if cfg!(feature = "derive") {
" (note: `Args` implicitly creates `ArgGroup`s; disable with `#[group(skip)]`"
} else {
""
};

// Name conflicts
assert!(
cmd.get_groups().filter(|x| x.id == group.id).count() < 2,
"Command {}: Argument group name must be unique\n\n\t'{}' is already in use",
"Command {}: Argument group name must be unique\n\n\t'{}' is already in use{}",
cmd.get_name(),
group.get_id(),
derive_hint
);

// Groups should not have naming conflicts with Args
assert!(
!cmd.get_arguments().any(|x| x.get_id() == group.get_id()),
"Command {}: Argument group name '{}' must not conflict with argument name",
"Command {}: Argument group name '{}' must not conflict with argument name{}",
cmd.get_name(),
group.get_id(),
derive_hint
);

for arg in &group.args {
Expand Down
29 changes: 29 additions & 0 deletions tests/derive/groups.rs
Expand Up @@ -90,3 +90,32 @@ fn skip_group_avoids_duplicate_ids() {
assert_eq!(Compose::<Empty, Empty>::group_id(), None);
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
}

#[test]
#[should_panic = "\
Command clap: Argument group name must be unique

\t'Compose' is already in use (note: `Args` implicitly creates `ArgGroup`s; disable with `#[group(skip)]`"]
fn helpful_panic_on_duplicate_groups() {
#[derive(Parser, Debug)]
struct Opt {
#[command(flatten)]
first: Compose<Empty, Empty>,
#[command(flatten)]
second: Compose<Empty, Empty>,
}

#[derive(clap::Args, Debug)]
pub struct Compose<L: clap::Args, R: clap::Args> {
#[clap(flatten)]
pub left: L,
#[clap(flatten)]
pub right: R,
}

#[derive(clap::Args, Clone, Copy, Debug)]
pub struct Empty;

use clap::CommandFactory;
Opt::command().debug_assert();
}