From a1d69ca824f27cb7351a06ec689838e99d2c1be5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Nov 2024 13:11:29 -0600 Subject: [PATCH] refactor(parser): Split up parsing from post-processing --- clap_builder/src/parser/parser.rs | 34 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/clap_builder/src/parser/parser.rs b/clap_builder/src/parser/parser.rs index 164a21c98f4..d820c63995b 100644 --- a/clap_builder/src/parser/parser.rs +++ b/clap_builder/src/parser/parser.rs @@ -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 = None; @@ -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); @@ -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(