From 0b06f2b4a8543a014afa15927728d16d9ba38eca Mon Sep 17 00:00:00 2001 From: Nikolai Date: Wed, 19 Apr 2023 05:06:32 +0100 Subject: [PATCH] fixing seq panic on none --- src/uu/seq/src/error.rs | 24 +++++++++++++++++++----- src/uu/seq/src/seq.rs | 11 +++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index ae641a978..ad6b5733a 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -25,6 +25,9 @@ pub enum SeqError { /// The parameter is the increment argument as a [`String`] as read /// from the command line. ZeroIncrement(String), + + /// No arguments were passed to this function, 1 or more is required + NoArguments, } impl SeqError { @@ -33,6 +36,7 @@ impl SeqError { match self { Self::ParseError(s, _) => s, Self::ZeroIncrement(s) => s, + Self::NoArguments => "", } } @@ -40,11 +44,12 @@ impl SeqError { 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", + ParseNumberError::Float => "invalid floating point argument: ", + ParseNumberError::Nan => "invalid 'not-a-number' argument: ", + ParseNumberError::Hex => "invalid hexadecimal argument: ", }, - Self::ZeroIncrement(_) => "Zero increment value", + Self::ZeroIncrement(_) => "invalid Zero increment value: ", + Self::NoArguments => "missing operand", } } } @@ -63,6 +68,15 @@ 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()) + write!( + f, + "{}{}", + self.argtype(), + if self.arg() != "" { + self.arg().quote().to_string() + } else { + "".to_string() + } + ) } } diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 97382ed1b..44e0a7744 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