1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

fix tr with any flag with more than 2 operands (#5952)

* fix tr

* add tests

* fix clippy

* fix clippy2

* do suggestions

* do suggestions

* remove mut

* tr: move var to block & remove its type

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
BaherSalama 2024-02-13 16:30:15 +02:00 committed by GitHub
parent 826cdbe3dc
commit 5603305e75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 7 deletions

View file

@ -57,15 +57,32 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if !(delete_flag || squeeze_flag) && sets_len < 2 { if !(delete_flag || squeeze_flag) && sets_len < 2 {
return Err(UUsageError::new( return Err(UUsageError::new(
1, 1,
format!("missing operand after {}", sets[0].quote()), format!(
"missing operand after {}\nTwo strings must be given when translating.",
sets[0].quote()
),
)); ));
} }
if delete_flag && !squeeze_flag && sets_len > 1 { if sets_len > 1 {
return Err(UUsageError::new( let start = "extra operand";
1, if delete_flag && !squeeze_flag {
format!("extra operand {}\nOnly one string may be given when deleting without squeezing repeats.", sets[1].quote()), let op = sets[1].quote();
)); let msg = if sets_len == 2 {
format!(
"{} {}\nOnly one string may be given when deleting without squeezing repeats.",
start, op,
)
} else {
format!("{} {}", start, op,)
};
return Err(UUsageError::new(1, msg));
}
if sets_len > 2 {
let op = sets[2].quote();
let msg = format!("{} {}", start, op);
return Err(UUsageError::new(1, msg));
}
} }
if let Some(first) = sets.first() { if let Some(first) = sets.first() {
@ -170,5 +187,5 @@ pub fn uu_app() -> Command {
.help("first truncate SET1 to length of SET2") .help("first truncate SET1 to length of SET2")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg(Arg::new(options::SETS).num_args(1..=2)) .arg(Arg::new(options::SETS).num_args(1..))
} }

View file

@ -1155,3 +1155,27 @@ fn test_delete_flag_takes_only_one_operand() {
"extra operand 'p'\nOnly one string may be given when deleting without squeezing repeats.", "extra operand 'p'\nOnly one string may be given when deleting without squeezing repeats.",
); );
} }
#[test]
fn test_truncate_flag_fails_with_more_than_two_operand() {
new_ucmd!()
.args(&["-t", "a", "b", "c"])
.fails()
.stderr_contains("extra operand 'c'");
}
#[test]
fn test_squeeze_flag_fails_with_more_than_two_operand() {
new_ucmd!()
.args(&["-s", "a", "b", "c"])
.fails()
.stderr_contains("extra operand 'c'");
}
#[test]
fn test_complement_flag_fails_with_more_than_two_operand() {
new_ucmd!()
.args(&["-c", "a", "b", "c"])
.fails()
.stderr_contains("extra operand 'c'");
}