Skip to content

Commit

Permalink
fix(parser): Set correct source on ignore-errors with did-you-mean
Browse files Browse the repository at this point in the history
Fixes #5867
  • Loading branch information
epage committed Jan 7, 2025
1 parent 49cd317 commit c721b6c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,9 +1571,11 @@ impl Parser<'_> {
);

// Add the arg to the matches to build a proper usage string
if let Some((name, _)) = did_you_mean.as_ref() {
if let Some(arg) = self.cmd.get_keymap().get(&name.as_ref()) {
self.start_custom_arg(matcher, arg, ValueSource::CommandLine);
if !self.cmd.is_ignore_errors_set() {
if let Some((name, _)) = did_you_mean.as_ref() {
if let Some(arg) = self.cmd.get_keymap().get(&name.as_ref()) {
self.start_custom_arg(matcher, arg, ValueSource::CommandLine);
}
}
}
let did_you_mean = did_you_mean.map(|(arg, cmd)| (format!("--{arg}"), cmd));
Expand Down
11 changes: 8 additions & 3 deletions tests/builder/ignore_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ fn did_you_mean() {
let r = cmd.try_get_matches_from_mut(vec!["cmd", "--ig"]);
assert!(r.is_err());
let err = r.unwrap_err();
utils::assert_error(err, ErrorKind::UnknownArgument, str![[r#"
utils::assert_error(
err,
ErrorKind::UnknownArgument,
str![[r#"
error: unexpected argument '--ig' found
tip: a similar argument exists: '--ignore-immutable'
Expand All @@ -135,7 +138,9 @@ Usage: cmd --ignore-immutable
For more information, try '--help'.
"#]], true);
"#]],
true,
);

let r = cmd
.ignore_errors(true)
Expand All @@ -145,7 +150,7 @@ For more information, try '--help'.
assert!(m.contains_id("ignore-immutable"), "{m:#?}");
assert_eq!(
m.value_source("ignore-immutable"),
Some(ValueSource::CommandLine)
Some(ValueSource::DefaultValue)
);
}

Expand Down

0 comments on commit c721b6c

Please sign in to comment.