From e5361a8c113cc02dda1d1feb8d6628427fb94c69 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Mon, 31 Jan 2022 19:04:32 -0500 Subject: [PATCH] split: correct error message on invalid arg. to -a Correct the error message displayed on an invalid parameter to the `--suffix-length` or `-a` command-line option. --- src/uu/split/src/split.rs | 12 +++++++----- tests/by-util/test_split.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index a05959810..9df7bd42c 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -233,12 +233,14 @@ struct Settings { impl Settings { /// Parse a strategy from the command-line arguments. fn from(matches: &ArgMatches) -> UResult { + let suffix_length_str = matches.value_of(OPT_SUFFIX_LENGTH).unwrap(); let result = Self { - suffix_length: matches - .value_of(OPT_SUFFIX_LENGTH) - .unwrap() - .parse() - .unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH)), + suffix_length: suffix_length_str.parse().map_err(|_| { + USimpleError::new( + 1, + format!("invalid suffix length: {}", suffix_length_str.quote()), + ) + })?, numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0, additional_suffix: matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(), verbose: matches.occurrences_of("verbose") > 0, diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 8e61c5153..911a7bf30 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -440,3 +440,12 @@ fn test_number() { assert_eq!(file_read("xad"), "pqrst"); assert_eq!(file_read("xae"), "uvwxyz"); } + +#[test] +fn test_invalid_suffix_length() { + new_ucmd!() + .args(&["-a", "xyz"]) + .fails() + .no_stdout() + .stderr_contains("invalid suffix length: 'xyz'"); +}