Skip to content

Commit

Permalink
fix(assert): Provide next steps for '-h' conflicts
Browse files Browse the repository at this point in the history
For now, we are going to provide a better debug assert in this case.
Resolving clap-rs#3405 is the better long term route.

Fixes clap-rs#3403
  • Loading branch information
epage committed Feb 9, 2022
1 parent df1d50c commit 6c60b79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/build/app/mod.rs
Expand Up @@ -2940,17 +2940,36 @@ impl<'help> App<'help> {
self.args.remove(index);
}
} else {
let other_arg_has_short = self.args.args().any(|x| x.short == Some('h'));
let help = self
.args
.args_mut()
.args()
.find(|x| x.id == Id::help_hash())
.expect(INTERNAL_ERROR_MSG);
assert_ne!(help.provider, ArgProvider::User);

if help.short.is_some() {
} else if !(other_arg_has_short
if help.short == Some('h') {
if let Some(other_arg) = self
.args
.args()
.find(|x| x.id != Id::help_hash() && x.short == Some('h'))
{
panic!(
"`help`s `-h` conflicts with `{}`.
To change `help`s short, call `app.arg(Arg::new(\"help\")...)`.",
other_arg.name
);
}
}
} else if !(self.args.args().any(|x| x.short == Some('h'))
|| self.subcommands.iter().any(|sc| sc.short_flag == Some('h')))
{
let help = self
.args
.args_mut()
.find(|x| x.id == Id::help_hash())
.expect(INTERNAL_ERROR_MSG);
help.short = Some('h');
} else {
debug!("App::_check_help_and_version: Removing `-h` from help");
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/help.rs
Expand Up @@ -1557,7 +1557,7 @@ fn arg_short_conflict_with_help() {

#[cfg(debug_assertions)]
#[test]
#[should_panic = "Short option names must be unique for each argument, but '-h' is in use by both 'home' and 'help'"]
#[should_panic = "`help`s `-h` conflicts with `home`."]
fn arg_short_conflict_with_help_mut_arg() {
let _ = App::new("conflict")
.arg(Arg::new("home").short('h'))
Expand Down

0 comments on commit 6c60b79

Please sign in to comment.