Merge pull request #2604 from epage/exit

fix(exit): Be consistent in exit code
This commit is contained in:
Pavan Kumar Sunkara 2021-07-25 14:32:47 +01:00 committed by GitHub
commit 79e3b37ecc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View file

@ -27,7 +27,7 @@ use crate::{
mkeymap::MKeyMap,
output::{fmt::Colorizer, Help, HelpWriter, Usage},
parse::{ArgMatcher, ArgMatches, Input, Parser},
util::{safe_exit, termcolor::ColorChoice, ArgStr, Id, Key},
util::{safe_exit, termcolor::ColorChoice, ArgStr, Id, Key, USAGE_CODE},
Result as ClapResult, INTERNAL_ERROR_MSG,
};
@ -1956,7 +1956,7 @@ impl<'help> App<'help> {
}
drop(e);
safe_exit(2);
safe_exit(USAGE_CODE);
}
e.exit()

View file

@ -12,7 +12,7 @@ use crate::{
build::Arg,
output::fmt::Colorizer,
parse::features::suggestions,
util::{safe_exit, termcolor::ColorChoice},
util::{safe_exit, termcolor::ColorChoice, SUCCESS_CODE, USAGE_CODE},
};
/// Short hand for [`Result`] type
@ -479,11 +479,11 @@ impl Error {
pub fn exit(&self) -> ! {
if self.use_stderr() {
self.message.print().expect("Error writing Error to stderr");
safe_exit(1);
safe_exit(USAGE_CODE);
}
self.message.print().expect("Error writing Error to stdout");
safe_exit(0)
safe_exit(SUCCESS_CODE)
}
/// Prints formatted and colored error to `stdout` or `stderr` according to its error kind

View file

@ -16,6 +16,14 @@ pub(crate) use termcolor;
#[cfg(not(feature = "color"))]
pub(crate) mod termcolor;
pub(crate) const SUCCESS_CODE: i32 = 0;
// While sysexists.h defines EX_USAGE as 64, this doesn't seem to be used much in practice but
// instead 2 seems to be frequently used.
// Examples
// - GNU `ls` returns 2
// - Python's `argparse` returns 2
pub(crate) const USAGE_CODE: i32 = 2;
pub(crate) fn safe_exit(code: i32) -> ! {
use std::io::Write;