echo: allow leading hyphens (#1887)

* fix: use settings to allow leading hyphen and trailing var arg

fixes: https://github.com/uutils/coreutils/issues/1873

* test: add test cases

* test: add more test cases with different order in hyphen values

* chore: add comment to explain why we need TrailingVarArg
This commit is contained in:
Yagiz Degirmenci 2021-03-23 11:40:05 +03:00 committed by GitHub
parent 9ffcfcd8bf
commit 5e2e2e8ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 0 deletions

View file

@ -117,6 +117,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let matches = App::new(executable!()) let matches = App::new(executable!())
.name(NAME) .name(NAME)
// TrailingVarArg specifies the final positional argument is a VarArg
// and it doesn't attempts the parse any further args.
// Final argument must have multiple(true) or the usage string equivalent.
.setting(clap::AppSettings::TrailingVarArg)
.setting(clap::AppSettings::AllowLeadingHyphen)
.version(VERSION) .version(VERSION)
.usage(SYNTAX) .usage(SYNTAX)
.about(SUMMARY) .about(SUMMARY)

View file

@ -173,3 +173,58 @@ fn test_disable_escapes() {
.succeeds() .succeeds()
.stdout_only(format!("{}\n", input_str)); .stdout_only(format!("{}\n", input_str));
} }
#[test]
fn test_hyphen_value() {
new_ucmd!().arg("-abc").succeeds().stdout_is("-abc\n");
}
#[test]
fn test_multiple_hyphen_values() {
new_ucmd!()
.args(&["-abc", "-def", "-edf"])
.succeeds()
.stdout_is("-abc -def -edf\n");
}
#[test]
fn test_hyphen_values_inside_string() {
new_ucmd!()
.arg("'\"\n'CXXFLAGS=-g -O2'\n\"'")
.succeeds()
.stdout
.contains("CXXFLAGS");
}
#[test]
fn test_hyphen_values_at_start() {
let result = new_ucmd!()
.arg("-E")
.arg("-test")
.arg("araba")
.arg("-merci")
.run();
assert!(result.success);
assert_eq!(false, result.stdout.contains("-E"));
assert_eq!(result.stdout, "-test araba -merci\n");
}
#[test]
fn test_hyphen_values_between() {
let result = new_ucmd!().arg("test").arg("-E").arg("araba").run();
assert!(result.success);
assert_eq!(result.stdout, "test -E araba\n");
let result = new_ucmd!()
.arg("dumdum ")
.arg("dum dum dum")
.arg("-e")
.arg("dum")
.run();
assert!(result.success);
assert_eq!(result.stdout, "dumdum dum dum dum -e dum\n");
assert_eq!(true, result.stdout.contains("-e"));
}