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

Fix tr argument handling for missing arguments (#1601)

* tests/tr ~ confirm failure for missing argument(s)

* fix/tr ~ mimic GNU error reponse for missing argument(s)
This commit is contained in:
Roy Ivy III 2020-10-02 15:43:57 -05:00 committed by GitHub
parent 9a1c560aba
commit 0fa249a944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 5 deletions

View file

@ -218,17 +218,29 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
return 0;
}
if matches.free.is_empty() {
usage(&opts);
return 1;
}
let dflag = matches.opt_present("d");
let cflag = matches.opts_present(&["c".to_owned(), "C".to_owned()]);
let sflag = matches.opt_present("s");
let tflag = matches.opt_present("t");
let sets = matches.free;
if sets.is_empty() {
show_error!(
"missing operand\nTry `{} --help` for more information.",
NAME
);
return 1;
}
if !(dflag || sflag) && sets.len() < 2 {
show_error!(
"missing operand after {}\nTry `{} --help` for more information.",
sets[0],
NAME
);
return 1;
}
if cflag && !dflag && !sflag {
show_error!("-c is only supported with -d or -s");
return 1;

View file

@ -116,3 +116,23 @@ fn test_truncate_with_set1_shorter_than_set2() {
.run()
.stdout_is("xycde");
}
#[test]
fn missing_args_fails() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.run();
assert!(!result.success);
assert!(result.stderr.contains("missing operand"));
}
#[test]
fn missing_required_second_arg_fails() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd
.args(&["foo"])
.run();
assert!(!result.success);
assert!(result.stderr.contains("missing operand after"));
}