diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index ae641a978c5..fc8452e1388 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -2,7 +2,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore numberparse argtype +// spell-checker:ignore numberparse //! Errors returned by seq. use std::error::Error; use std::fmt::Display; @@ -25,29 +25,11 @@ pub enum SeqError { /// The parameter is the increment argument as a [`String`] as read /// from the command line. ZeroIncrement(String), -} - -impl SeqError { - /// The [`String`] argument as read from the command-line. - fn arg(&self) -> &str { - match self { - Self::ParseError(s, _) => s, - Self::ZeroIncrement(s) => s, - } - } - /// The type of argument that is causing the error. - fn argtype(&self) -> &str { - match self { - Self::ParseError(_, e) => match e { - ParseNumberError::Float => "floating point argument", - ParseNumberError::Nan => "'not-a-number' argument", - ParseNumberError::Hex => "hexadecimal argument", - }, - Self::ZeroIncrement(_) => "Zero increment value", - } - } + /// No arguments were passed to this function, 1 or more is required + NoArguments, } + impl UError for SeqError { /// Always return 1. fn code(&self) -> i32 { @@ -63,6 +45,17 @@ impl Error for SeqError {} impl Display for SeqError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "invalid {}: {}", self.argtype(), self.arg().quote()) + match self { + Self::ParseError(s, e) => { + let error_type = match e { + ParseNumberError::Float => "floating point", + ParseNumberError::Nan => "'not-a-number'", + ParseNumberError::Hex => "hexadecimal", + }; + write!(f, "invalid {error_type} argument: {}", s.quote()) + } + Self::ZeroIncrement(s) => write!(f, "invalid Zero increment value: {}", s.quote()), + Self::NoArguments => write!(f, "missing operand"), + } } } diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 4562ddb7d01..2e55efa4ada 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -58,10 +58,13 @@ type RangeFloat = (ExtendedBigDecimal, ExtendedBigDecimal, ExtendedBigDecimal); pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; - let numbers = matches - .get_many::(ARG_NUMBERS) - .unwrap() - .collect::>(); + let numbers_option = matches.get_many::(ARG_NUMBERS); + + if numbers_option.is_none() { + return Err(SeqError::NoArguments.into()); + } + + let numbers = numbers_option.unwrap().collect::>(); let options = SeqOptions { separator: matches diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 02509b3b5c6..de078191251 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -7,6 +7,14 @@ fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } +#[test] +fn test_no_args() { + new_ucmd!() + .fails() + .code_is(1) + .stderr_contains("missing operand"); +} + #[test] fn test_hex_rejects_sign_after_identifier() { new_ucmd!()