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

Extract/add public method Error.exit_code #4967

Merged
merged 1 commit into from Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions clap_builder/src/error/mod.rs
Expand Up @@ -214,21 +214,26 @@ impl<F: ErrorFormatter> Error<F> {
}
}

/// Returns the exit code that `.exit` will exit the process with.
///
/// When the error's kind would print to `stderr` this returns `2`,
/// else it returns `0`.
pub fn exit_code(&self) -> i32 {
if self.use_stderr() {
USAGE_CODE
} else {
SUCCESS_CODE
}
}

/// Prints the error and exits.
///
/// Depending on the error kind, this either prints to `stderr` and exits with a status of `2`
/// or prints to `stdout` and exits with a status of `0`.
pub fn exit(&self) -> ! {
if self.use_stderr() {
// Swallow broken pipe errors
let _ = self.print();

safe_exit(USAGE_CODE);
}

// Swallow broken pipe errors
let _ = self.print();
safe_exit(SUCCESS_CODE)
safe_exit(self.exit_code())
}

/// Prints formatted and colored error to `stdout` or `stderr` according to its error kind
Expand Down