From d451e0a60cfed489999fbccd9334a4a680dc3583 Mon Sep 17 00:00:00 2001 From: Sebastian Toh Date: Wed, 28 Jun 2023 08:51:20 +0800 Subject: [PATCH 1/2] test(ignore_errors): Add help & version cmd tests --- tests/builder/ignore_errors.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/builder/ignore_errors.rs b/tests/builder/ignore_errors.rs index 3103ad519ad..a01f00aea8c 100644 --- a/tests/builder/ignore_errors.rs +++ b/tests/builder/ignore_errors.rs @@ -1,5 +1,7 @@ use clap::{arg, Arg, ArgAction, Command}; +use super::utils; + #[test] fn single_short_arg_without_value() { let cmd = Command::new("cmd").ignore_errors(true).arg(arg!( @@ -124,3 +126,26 @@ fn subcommand() { Some("some other val") ); } + +#[test] +#[should_panic(expected = "`Result::unwrap_err()` on an `Ok` value")] +fn help_command() { + static HELP: &str = "\ +Usage: test + +Options: + -h, --help Print help +"; + + let cmd = Command::new("test").ignore_errors(true); + + utils::assert_output(cmd, "test --help", HELP, false); +} + +#[test] +#[should_panic(expected = "`Result::unwrap_err()` on an `Ok` value")] +fn version_command() { + let cmd = Command::new("test").ignore_errors(true).version("0.1"); + + utils::assert_output(cmd, "test --version", "test 0.1\n", false); +} From 8103e9760a9e6267490b5c09439416ef575d5991 Mon Sep 17 00:00:00 2001 From: Sebastian Toh Date: Wed, 28 Jun 2023 08:56:05 +0800 Subject: [PATCH 2/2] fix(ignore_errors): Allow help and version command --- clap_builder/src/builder/command.rs | 2 +- tests/builder/ignore_errors.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 5756e7dfd84..71ea73c0e3b 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -3810,7 +3810,7 @@ impl Command { // do the real parsing let mut parser = Parser::new(self); if let Err(error) = parser.get_matches_with(&mut matcher, raw_args, args_cursor) { - if self.is_set(AppSettings::IgnoreErrors) { + if self.is_set(AppSettings::IgnoreErrors) && error.use_stderr() { debug!("Command::_do_parse: ignoring error: {error}"); } else { return Err(error); diff --git a/tests/builder/ignore_errors.rs b/tests/builder/ignore_errors.rs index a01f00aea8c..014149948df 100644 --- a/tests/builder/ignore_errors.rs +++ b/tests/builder/ignore_errors.rs @@ -128,7 +128,6 @@ fn subcommand() { } #[test] -#[should_panic(expected = "`Result::unwrap_err()` on an `Ok` value")] fn help_command() { static HELP: &str = "\ Usage: test @@ -143,7 +142,6 @@ Options: } #[test] -#[should_panic(expected = "`Result::unwrap_err()` on an `Ok` value")] fn version_command() { let cmd = Command::new("test").ignore_errors(true).version("0.1");