From 36329896e733272b192b404cbb9ba26e82bb2742 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Wed, 14 Jun 2023 01:17:24 +0200 Subject: [PATCH] feat: Extract/add public method Error.exit_code Simplifies method Error.exit as a side effect. --- clap_builder/src/error/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/clap_builder/src/error/mod.rs b/clap_builder/src/error/mod.rs index fa8e9e48454..210a1717f1f 100644 --- a/clap_builder/src/error/mod.rs +++ b/clap_builder/src/error/mod.rs @@ -214,21 +214,26 @@ impl Error { } } + /// 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