Skip to content

Commit

Permalink
Merge pull request #4988 from sebastiantoh/ignore-errors
Browse files Browse the repository at this point in the history
fix(parser): Allow help and version command
  • Loading branch information
epage authored Jun 28, 2023
2 parents 1f71fd9 + 8103e97 commit d83cc6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions tests/builder/ignore_errors.rs
Original file line number Diff line number Diff line change
@@ -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!(
Expand Down Expand Up @@ -124,3 +126,24 @@ fn subcommand() {
Some("some other val")
);
}

#[test]
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]
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);
}

0 comments on commit d83cc6d

Please sign in to comment.