fixing seq panic on none

This commit is contained in:
Nikolai 2023-04-19 05:06:32 +01:00 committed by Sylvestre Ledru
parent ab8aa32e60
commit 0b06f2b4a8
2 changed files with 26 additions and 9 deletions

View file

@ -25,6 +25,9 @@ pub enum SeqError {
/// The parameter is the increment argument as a [`String`] as read /// The parameter is the increment argument as a [`String`] as read
/// from the command line. /// from the command line.
ZeroIncrement(String), ZeroIncrement(String),
/// No arguments were passed to this function, 1 or more is required
NoArguments,
} }
impl SeqError { impl SeqError {
@ -33,6 +36,7 @@ impl SeqError {
match self { match self {
Self::ParseError(s, _) => s, Self::ParseError(s, _) => s,
Self::ZeroIncrement(s) => s, Self::ZeroIncrement(s) => s,
Self::NoArguments => "",
} }
} }
@ -40,11 +44,12 @@ impl SeqError {
fn argtype(&self) -> &str { fn argtype(&self) -> &str {
match self { match self {
Self::ParseError(_, e) => match e { Self::ParseError(_, e) => match e {
ParseNumberError::Float => "floating point argument", ParseNumberError::Float => "invalid floating point argument: ",
ParseNumberError::Nan => "'not-a-number' argument", ParseNumberError::Nan => "invalid 'not-a-number' argument: ",
ParseNumberError::Hex => "hexadecimal 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 { impl Display for SeqError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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()
}
)
} }
} }

View file

@ -58,10 +58,13 @@ type RangeFloat = (ExtendedBigDecimal, ExtendedBigDecimal, ExtendedBigDecimal);
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?; let matches = uu_app().try_get_matches_from(args)?;
let numbers = matches let numbers_option = matches.get_many::<String>(ARG_NUMBERS);
.get_many::<String>(ARG_NUMBERS)
.unwrap() if numbers_option.is_none() {
.collect::<Vec<_>>(); return Err(SeqError::NoArguments.into());
}
let numbers = numbers_option.unwrap().collect::<Vec<_>>();
let options = SeqOptions { let options = SeqOptions {
separator: matches separator: matches