From f53f9026fcad6d195ddbabb6c17750137ceaadd2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 12 Nov 2024 21:09:55 -0600 Subject: [PATCH] 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. --- clap_builder/src/parser/parser.rs | 10 +++++++++- tests/builder/ignore_errors.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clap_builder/src/parser/parser.rs b/clap_builder/src/parser/parser.rs index d820c63995b..81e47a004b5 100644 --- a/clap_builder/src/parser/parser.rs +++ b/clap_builder/src/parser/parser.rs @@ -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")] diff --git a/tests/builder/ignore_errors.rs b/tests/builder/ignore_errors.rs index 850699a21b0..702b7746e63 100644 --- a/tests/builder/ignore_errors.rs +++ b/tests/builder/ignore_errors.rs @@ -113,7 +113,7 @@ fn unexpected_argument() { m.get_one::("config").cloned(), Some("config file".to_owned()) ); - assert_eq!(m.get_one::("unset-flag").copied(), None); + assert_eq!(m.get_one::("unset-flag").copied(), Some(false)); } #[test]