Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- *(assert)* Add missing `#[track_caller]`s to make it easier to debug asserts
- *(assert)* Ensure `overrides_with` IDs are valid
- *(assert)* Ensure no self-`overrides_with` now that Actions replace it
- *(assert)* Ensure subcommand names are not duplicated
- *(help)* Use `Command::display_name` in the help title rather than `Command::bin_name`
- *(version)* Use `Command::display_name` rather than `Command::bin_name`
- *(parser)* Assert on unknown args when using external subcommands (#3703)
Expand Down
19 changes: 19 additions & 0 deletions src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,25 @@ pub(crate) fn assert_app(cmd: &Command) {
detect_duplicate_flags(&long_flags, "long");
detect_duplicate_flags(&short_flags, "short");

let mut subs = indexmap::IndexSet::new();
for sc in cmd.get_subcommands() {
assert!(
subs.insert(sc.get_name()),
"Command {}: command name `{}` is duplicated",
cmd.get_name(),
sc.get_name()
);
for alias in sc.get_all_aliases() {
assert!(
subs.insert(alias),
"Command {}: command `{}` alias `{}` is duplicated",
cmd.get_name(),
sc.get_name(),
alias
);
}
}

_verify_positionals(cmd);

if let Some(help_template) = cmd.get_help_template() {
Expand Down
18 changes: 18 additions & 0 deletions tests/builder/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,21 @@ OPTIONS:
subcmd.write_help(&mut buf).unwrap();
utils::assert_eq(EXPECTED, String::from_utf8(buf).unwrap());
}

#[test]
#[should_panic = "Command test: command name `repeat` is duplicated"]
fn duplicate_subcommand() {
Command::new("test")
.subcommand(Command::new("repeat"))
.subcommand(Command::new("repeat"))
.build()
}

#[test]
#[should_panic = "Command test: command `unique` alias `repeat` is duplicated"]
fn duplicate_subcommand_alias() {
Command::new("test")
.subcommand(Command::new("repeat"))
.subcommand(Command::new("unique").alias("repeat"))
.build()
}