1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #2539 from miDeb/sort/args-exit-code

sort: do not exit with failure for "--version" or "--help"
This commit is contained in:
Sylvestre Ledru 2021-08-01 13:50:48 +02:00 committed by GitHub
commit 11d03bffd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View file

@ -949,9 +949,23 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let usage = get_usage();
let mut settings: GlobalSettings = Default::default();
let matches = uu_app().usage(&usage[..]).get_matches_from_safe(args);
let matches = crash_if_err!(2, matches);
let matches = match uu_app().usage(&usage[..]).get_matches_from_safe(args) {
Ok(t) => t,
Err(e) => {
// not all clap "Errors" are because of a failure to parse arguments.
// "--version" also causes an Error to be returned, but we should not print to stderr
// nor return with a non-zero exit code in this case (we should print to stdout and return 0).
// This logic is similar to the code in clap, but we return 2 as the exit code in case of real failure
// (clap returns 1).
if e.use_stderr() {
eprintln!("{}", e.message);
return 2;
} else {
println!("{}", e.message);
return 0;
}
}
};
settings.debug = matches.is_present(options::DEBUG);

View file

@ -1020,3 +1020,20 @@ fn test_separator_null() {
.succeeds()
.stdout_only("a\0z\0z\nz\0b\0a\nz\0a\0b\n");
}
#[test]
fn test_no_error_for_version() {
new_ucmd!()
.arg("--version")
.succeeds()
.stdout_contains("sort");
}
#[test]
fn test_wrong_args_exit_code() {
new_ucmd!()
.arg("--misspelled")
.fails()
.status_code(2)
.stderr_contains("--misspelled");
}