Skip to content

Commit

Permalink
fix(parser): Fill in defaults on ignored error
Browse files Browse the repository at this point in the history
This came up in #5812 and is especially problematic for derives.

Not really a fan of this solution but its the least invasive.
I also considered going wild with error recovery or moving towards a
solution for #1546.
  • Loading branch information
epage committed Nov 13, 2024
1 parent a1d69ca commit f53f902
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ impl<'cmd> Parser<'cmd> {
) -> ClapResult<()> {
debug!("Parser::get_matches_with");

ok!(self.parse(matcher, raw_args, args_cursor));
ok!(self
.parse(matcher, raw_args, args_cursor)
.inspect_err(|_err| {
if self.cmd.is_ignore_errors_set() {
#[cfg(feature = "env")]
let _ = self.add_env(matcher);
let _ = self.add_defaults(matcher);
}
}));
ok!(self.resolve_pending(matcher));

#[cfg(feature = "env")]
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/ignore_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn unexpected_argument() {
m.get_one::<String>("config").cloned(),
Some("config file".to_owned())
);
assert_eq!(m.get_one::<bool>("unset-flag").copied(), None);
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
}

#[test]
Expand Down

0 comments on commit f53f902

Please sign in to comment.