Skip to content

Commit

Permalink
refactor(parser): Split up parsing from post-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 12, 2024
1 parent 6827841 commit a1d69ca
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,29 @@ 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");

ok!(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));

Validator::new(self.cmd).validate(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<()> {
debug!("Parser::parse");
// Verify all positional assertions pass

let mut subcmd_name: Option<String> = None;
Expand Down Expand Up @@ -436,11 +456,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(matcher);
return Ok(());
} else {
// Start error processing
let _ = self.resolve_pending(matcher);
Expand Down Expand Up @@ -474,11 +490,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(matcher)
Ok(())
}

fn match_arg_error(
Expand Down

0 comments on commit a1d69ca

Please sign in to comment.