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
/// 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()
}
)
}
}

View file

@ -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::<String>(ARG_NUMBERS)
.unwrap()
.collect::<Vec<_>>();
let numbers_option = matches.get_many::<String>(ARG_NUMBERS);
if numbers_option.is_none() {
return Err(SeqError::NoArguments.into());
}
let numbers = numbers_option.unwrap().collect::<Vec<_>>();
let options = SeqOptions {
separator: matches