Skip to content

Commit

Permalink
fix tr with delete flag if more than 1 operand given (#5945)
Browse files Browse the repository at this point in the history
* fix tr

* fix

* adding a test

* tr: rename test function

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
  • Loading branch information
BaherSalama and cakebaker committed Feb 6, 2024
1 parent 4c187e2 commit 5c2ae5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uu/tr/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
));
}

if delete_flag && !squeeze_flag && sets_len > 1 {
return Err(UUsageError::new(
1,
format!("extra operand {}\nOnly one string may be given when deleting without squeezing repeats.", sets[1].quote()),
));
}

if let Some(first) = sets.first() {
if first.ends_with('\\') {
show!(USimpleError::new(
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,15 @@ fn check_against_gnu_tr_tests_no_abort_1() {
.succeeds()
.stdout_is("abb");
}

#[test]
fn test_delete_flag_takes_only_one_operand() {
// gnu tr -d fails with more than 1 argument
new_ucmd!()
.args(&["-d", "a", "p"])
.pipe_in("abc")
.fails()
.stderr_contains(
"extra operand 'p'\nOnly one string may be given when deleting without squeezing repeats.",
);
}

0 comments on commit 5c2ae5b

Please sign in to comment.