1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #4750 from NikolaiSch/seq-panic-fix

fix: seq panic on no arguments #4749
This commit is contained in:
Daniel Hofstetter 2023-07-09 14:19:07 +02:00 committed by GitHub
commit f8a955266e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 27 deletions

View file

@ -2,7 +2,7 @@
// * // *
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * file that was distributed with this source code.
// spell-checker:ignore numberparse argtype // spell-checker:ignore numberparse
//! Errors returned by seq. //! Errors returned by seq.
use std::error::Error; use std::error::Error;
use std::fmt::Display; use std::fmt::Display;
@ -25,29 +25,11 @@ 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 {
/// 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",
}
}
}
impl UError for SeqError { impl UError for SeqError {
/// Always return 1. /// Always return 1.
fn code(&self) -> i32 { fn code(&self) -> i32 {
@ -63,6 +45,17 @@ 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()) 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"),
}
} }
} }

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

View file

@ -7,6 +7,14 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1); 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] #[test]
fn test_hex_rejects_sign_after_identifier() { fn test_hex_rejects_sign_after_identifier() {
new_ucmd!() new_ucmd!()