Skip to content

Commit 3632989

Browse files
committed
feat: Extract/add public method Error.exit_code
Simplifies method Error.exit as a side effect.
1 parent 1db9df4 commit 3632989

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

clap_builder/src/error/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,26 @@ impl<F: ErrorFormatter> Error<F> {
214214
}
215215
}
216216

217+
/// Returns the exit code that `.exit` will exit the process with.
218+
///
219+
/// When the error's kind would print to `stderr` this returns `2`,
220+
/// else it returns `0`.
221+
pub fn exit_code(&self) -> i32 {
222+
if self.use_stderr() {
223+
USAGE_CODE
224+
} else {
225+
SUCCESS_CODE
226+
}
227+
}
228+
217229
/// Prints the error and exits.
218230
///
219231
/// Depending on the error kind, this either prints to `stderr` and exits with a status of `2`
220232
/// or prints to `stdout` and exits with a status of `0`.
221233
pub fn exit(&self) -> ! {
222-
if self.use_stderr() {
223-
// Swallow broken pipe errors
224-
let _ = self.print();
225-
226-
safe_exit(USAGE_CODE);
227-
}
228-
229234
// Swallow broken pipe errors
230235
let _ = self.print();
231-
safe_exit(SUCCESS_CODE)
236+
safe_exit(self.exit_code())
232237
}
233238

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

0 commit comments

Comments
 (0)