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:
commit
11d03bffd3
2 changed files with 34 additions and 3 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue