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.
  • Loading branch information
epage committed Nov 11, 2024
1 parent 81a782b commit 2573e85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,30 @@ impl<'cmd> Parser<'cmd> {
&mut self,
matcher: &mut ArgMatcher,
raw_args: &mut clap_lex::RawArgs,
mut args_cursor: clap_lex::ArgCursor,
args_cursor: clap_lex::ArgCursor,
) -> ClapResult<()> {
debug!("Parser::get_matches_with");

let res = self.parse(matcher, raw_args, args_cursor);

ok!(self.resolve_pending(matcher));
#[cfg(feature = "env")]
ok!(self.add_env(matcher));
ok!(self.add_defaults(matcher));

let parse_state = res?;
Validator::new(self.cmd).validate(parse_state, matcher)
}

// The actual parsing function
#[allow(clippy::cognitive_complexity)]
pub(crate) fn parse(
&mut self,
matcher: &mut ArgMatcher,
raw_args: &mut clap_lex::RawArgs,
mut args_cursor: clap_lex::ArgCursor,
) -> ClapResult<ParseState> {
debug!("Parser::parse");
// Verify all positional assertions pass

let mut subcmd_name: Option<String> = None;
Expand Down Expand Up @@ -436,11 +457,7 @@ impl<'cmd> Parser<'cmd> {
matches: sc_m.into_inner(),
});

ok!(self.resolve_pending(matcher));
#[cfg(feature = "env")]
ok!(self.add_env(matcher));
ok!(self.add_defaults(matcher));
return Validator::new(self.cmd).validate(parse_state, matcher);
return Ok(parse_state);
} else {
// Start error processing
let _ = self.resolve_pending(matcher);
Expand Down Expand Up @@ -474,11 +491,7 @@ impl<'cmd> Parser<'cmd> {
ok!(self.parse_subcommand(&sc_name, matcher, raw_args, args_cursor, keep_state));
}

ok!(self.resolve_pending(matcher));
#[cfg(feature = "env")]
ok!(self.add_env(matcher));
ok!(self.add_defaults(matcher));
Validator::new(self.cmd).validate(parse_state, matcher)
Ok(parse_state)
}

fn match_arg_error(
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 2573e85

Please sign in to comment.