mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +00:00
Merge pull request #4967 from hartwork/error-exit-code
Extract/add public method `Error.exit_code`
This commit is contained in:
commit
a3f482f1d7
1 changed files with 13 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue