Skip to content

Commit

Permalink
Merge pull request clap-rs#4307 from epage/assert
Browse files Browse the repository at this point in the history
feat(assert): Help people know about implicit ArgGroups
  • Loading branch information
epage authored Sep 30, 2022
2 parents e9c7ee4 + 1065d6c commit 6328c14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}

0 comments on commit 6328c14

Please sign in to comment.