Skip to content

Commit 479df35

Browse files
committed
fix(parser): Fill in defaults on ignored error
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.
1 parent a1d69ca commit 479df35

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

clap_builder/src/parser/parser.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ impl<'cmd> Parser<'cmd> {
5454
) -> ClapResult<()> {
5555
debug!("Parser::get_matches_with");
5656

57-
ok!(self.parse(matcher, raw_args, args_cursor));
57+
ok!(self.parse(matcher, raw_args, args_cursor).map_err(|err| {
58+
if self.cmd.is_ignore_errors_set() {
59+
#[cfg(feature = "env")]
60+
let _ = self.add_env(matcher);
61+
let _ = self.add_defaults(matcher);
62+
}
63+
err
64+
}));
5865
ok!(self.resolve_pending(matcher));
5966

6067
#[cfg(feature = "env")]

tests/builder/ignore_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn unexpected_argument() {
113113
m.get_one::<String>("config").cloned(),
114114
Some("config file".to_owned())
115115
);
116-
assert_eq!(m.get_one::<bool>("unset-flag").copied(), None);
116+
assert_eq!(m.get_one::<bool>("unset-flag").copied(), Some(false));
117117
}
118118

119119
#[test]

0 commit comments

Comments
 (0)