From d9b6675bbf512687df43f598f8fc3d858f380ffe Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 23 Feb 2024 02:13:21 +0100 Subject: [PATCH 1/5] tr: enable passing -c multiple times --- src/uu/tr/src/tr.rs | 3 ++- tests/by-util/test_tr.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 57acb7fac..90096c90b 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -170,7 +170,8 @@ pub fn uu_app() -> Command { .short('c') .long(options::COMPLEMENT) .help("use the complement of SET1") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::COMPLEMENT), ) .arg( Arg::new(options::DELETE) diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index bf589a5c5..4ed3d3967 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -118,6 +118,15 @@ fn test_complement5() { .stdout_is("0a1b2c3"); } +#[test] +fn test_complement_multi_early() { + new_ucmd!() + .args(&["-c", "-c", "a", "X"]) + .pipe_in("ab") + .succeeds() + .stdout_is("aX"); +} + #[test] fn test_squeeze() { new_ucmd!() From cad94a69be5fe1707a607899e1b7d6b27949c443 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 23 Feb 2024 02:15:53 +0100 Subject: [PATCH 2/5] tr: prevent passing options in the wrong place Note: This requires using the DEPRECATED item Command::trailing_var_arg in clap. This is going to be another [problem with clap](https://github.com/tertsdiepraam/uutils-args/blob/main/docs/design/problems_with_clap.md). --- src/uu/tr/src/tr.rs | 1 + tests/by-util/test_tr.rs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 90096c90b..8b66d360c 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -164,6 +164,7 @@ pub fn uu_app() -> Command { .about(ABOUT) .override_usage(format_usage(USAGE)) .infer_long_args(true) + .trailing_var_arg(true) .arg( Arg::new(options::COMPLEMENT) .visible_short_alias('C') diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index 4ed3d3967..1086dee6a 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -78,6 +78,14 @@ fn test_complement1() { .stdout_is("aX"); } +#[test] +fn test_complement_afterwards_is_not_flag() { + new_ucmd!() + .args(&["a", "X", "-c"]) + .fails() + .stderr_contains("extra operand '-c'"); +} + #[test] fn test_complement2() { new_ucmd!() @@ -127,6 +135,22 @@ fn test_complement_multi_early() { .stdout_is("aX"); } +#[test] +fn test_complement_multi_middle() { + new_ucmd!() + .args(&["-c", "a", "-c", "X"]) + .fails() + .stderr_contains("tr: extra operand 'X'"); +} + +#[test] +fn test_complement_multi_late() { + new_ucmd!() + .args(&["-c", "a", "X", "-c"]) + .fails() + .stderr_contains("tr: extra operand '-c'"); +} + #[test] fn test_squeeze() { new_ucmd!() From dc664006feb59a67e674ac59cb09d380f5e21991 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 23 Feb 2024 02:24:02 +0100 Subject: [PATCH 3/5] tr: enable passing -d multiple times --- src/uu/tr/src/tr.rs | 3 ++- tests/by-util/test_tr.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 8b66d360c..ec3fc6763 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -179,7 +179,8 @@ pub fn uu_app() -> Command { .short('d') .long(options::DELETE) .help("delete characters in SET1, do not translate") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::DELETE), ) .arg( Arg::new(options::SQUEEZE) diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index 1086dee6a..da55daa1c 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -46,6 +46,32 @@ fn test_delete() { .stdout_is("BD"); } +#[test] +fn test_delete_afterwards_is_not_flag() { + new_ucmd!() + .args(&["a-z", "-d"]) + .pipe_in("aBcD") + .succeeds() + .stdout_is("-BdD"); +} + +#[test] +fn test_delete_multi() { + new_ucmd!() + .args(&["-d", "-d", "a-z"]) + .pipe_in("aBcD") + .succeeds() + .stdout_is("BD"); +} + +#[test] +fn test_delete_late() { + new_ucmd!() + .args(&["-d", "a-z", "-d"]) + .fails() + .stderr_contains("extra operand '-d'"); +} + #[test] fn test_delete_complement() { new_ucmd!() From 268af90843ca8b047a723ca38b88c970db31007e Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 24 Feb 2024 18:23:51 +0100 Subject: [PATCH 4/5] tr: enable passing -s multiple times --- src/uu/tr/src/tr.rs | 3 ++- tests/by-util/test_tr.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index ec3fc6763..df603427f 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -191,7 +191,8 @@ pub fn uu_app() -> Command { listed in the last specified SET, with a single occurrence \ of that character", ) - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::SQUEEZE), ) .arg( Arg::new(options::TRUNCATE_SET1) diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index da55daa1c..4825fa958 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -182,7 +182,16 @@ fn test_squeeze() { new_ucmd!() .args(&["-s", "a-z"]) .pipe_in("aaBBcDcc") - .run() + .succeeds() + .stdout_is("aBBcDc"); +} + +#[test] +fn test_squeeze_multi() { + new_ucmd!() + .args(&["-ss", "-s", "a-z"]) + .pipe_in("aaBBcDcc") + .succeeds() .stdout_is("aBBcDc"); } @@ -191,7 +200,16 @@ fn test_squeeze_complement() { new_ucmd!() .args(&["-sc", "a-z"]) .pipe_in("aaBBcDcc") - .run() + .succeeds() + .stdout_is("aaBcDcc"); +} + +#[test] +fn test_squeeze_complement_multi() { + new_ucmd!() + .args(&["-scsc", "a-z"]) // spell-checker:disable-line + .pipe_in("aaBBcDcc") + .succeeds() .stdout_is("aaBcDcc"); } From 44310f426c3ac36083af4fb145f8d0e56feae4e4 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 24 Feb 2024 18:26:30 +0100 Subject: [PATCH 5/5] tr: enable passing -t multiple times --- src/uu/tr/src/tr.rs | 3 ++- tests/by-util/test_tr.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index df603427f..968682a26 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -199,7 +199,8 @@ pub fn uu_app() -> Command { .long(options::TRUNCATE_SET1) .short('t') .help("first truncate SET1 to length of SET2") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::TRUNCATE_SET1), ) .arg(Arg::new(options::SETS).num_args(1..)) } diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index 4825fa958..6adbc4022 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -300,7 +300,16 @@ fn test_truncate() { new_ucmd!() .args(&["-t", "abc", "xy"]) .pipe_in("abcde") - .run() + .succeeds() + .stdout_is("xycde"); // spell-checker:disable-line +} + +#[test] +fn test_truncate_multi() { + new_ucmd!() + .args(&["-tt", "-t", "abc", "xy"]) + .pipe_in("abcde") + .succeeds() .stdout_is("xycde"); // spell-checker:disable-line }