From db79b5abb2938f92ed0115030aea0f2c94c07ff0 Mon Sep 17 00:00:00 2001 From: Hanif Bin Ariffin Date: Fri, 9 Jul 2021 23:22:12 +0800 Subject: [PATCH] `tr` should error out when if user provides more than 2 sets Signed-off-by: Hanif Bin Ariffin --- src/uu/tr/src/tr.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 28ce70c22..7df404f6f 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -235,7 +235,7 @@ fn get_usage() -> String { } fn get_long_usage() -> String { - String::from( + format!( "Translate, squeeze, and/or delete characters from standard input, writing to standard output.", ) @@ -259,7 +259,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let squeeze_flag = matches.is_present(options::SQUEEZE); let truncate_flag = matches.is_present(options::TRUNCATE); - let sets: Vec = match matches.values_of(options::SETS) { + let sets = match matches.values_of(options::SETS) { Some(v) => v.map(|v| v.to_string()).collect(), None => vec![], }; @@ -281,6 +281,15 @@ pub fn uumain(args: impl uucore::Args) -> i32 { return 1; } + if sets.len() > 2 { + show_error!( + "extra operand '{}'\nTry `{} --help` for more information.", + sets[2], + executable!() + ); + return 1; + } + let stdin = stdin(); let mut locked_stdin = stdin.lock(); let stdout = stdout();