Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Allow help and version command #4988

Merged
merged 2 commits into from
Jun 28, 2023
Merged

fix(parser): Allow help and version command #4988

merged 2 commits into from
Jun 28, 2023

Conversation

sebastiantoh
Copy link
Contributor

@sebastiantoh sebastiantoh commented Jun 27, 2023

Fixes #4392

Context

The help and version command are considered errors, though they are not "true" ones (I'm curious to know why they're considered errors to begin with 🤔 )

  • /// Not a true "error" as it means `--help` or similar was used.
    /// The help message will be sent to `stdout`.
    ///
    /// **Note**: If the help is displayed due to an error (such as missing subcommands) it will
    /// be sent to `stderr` instead of `stdout`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "help")] {
    /// # use clap_builder as clap;
    /// # use clap::{Command, Arg, error::ErrorKind};
    /// let result = Command::new("prog")
    /// .try_get_matches_from(vec!["prog", "--help"]);
    /// assert!(result.is_err());
    /// assert_eq!(result.unwrap_err().kind(), ErrorKind::DisplayHelp);
    /// # }
    /// ```
    DisplayHelp,
  • /// Not a true "error" as it means `--version` or similar was used.
    /// The message will be sent to `stdout`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use clap_builder as clap;
    /// # use clap::{Command, Arg, error::ErrorKind};
    /// let result = Command::new("prog")
    /// .version("3.0")
    /// .try_get_matches_from(vec!["prog", "--version"]);
    /// assert!(result.is_err());
    /// assert_eq!(result.unwrap_err().kind(), ErrorKind::DisplayVersion);
    /// ```
    DisplayVersion,

As a result, when ignore_errors is true, the output from the help and version commands are ignored.

Changes in this PR

This PR adds an explicit check so that only "true" errors are ignored

@sebastiantoh sebastiantoh changed the title fix: display help and version command even if ignore_errors is true fix: Allow help, version even when ignore_errors Jun 27, 2023
@epage
Copy link
Member

epage commented Jun 27, 2023

I'm curious to know why they're considered errors to begin with thinking

We want to unwind the parsing and the application to a level that can decide how to handle it (e.g. exit the process). While this isn't a failure, it mirrors what the semantics of Result::Err

@sebastiantoh sebastiantoh changed the title fix: Allow help, version even when ignore_errors fix(ignore_errors): Allow help and version command Jun 28, 2023
@sebastiantoh
Copy link
Contributor Author

thanks for the review and explanations! i've made the requested changes

@epage epage changed the title fix(ignore_errors): Allow help and version command fix(parser): Allow help and version command Jun 28, 2023
@epage epage merged commit d83cc6d into clap-rs:master Jun 28, 2023
arxanas added a commit to arxanas/git-branchless that referenced this pull request Jul 3, 2023
This was broken in clap-rs/clap#4988, although the PR seems to be legitimately correcting broken behavior.
arxanas added a commit to arxanas/git-branchless that referenced this pull request Jul 3, 2023
This was broken in clap-rs/clap#4988, although the PR seems to be legitimately correcting broken behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ignore errors disables help flag
2 participants