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

nl: show error if --join-blank-lines is zero

This commit is contained in:
Daniel Hofstetter 2023-07-20 15:26:46 +02:00
parent 8d5404cb57
commit aef130dae7
3 changed files with 29 additions and 14 deletions

View file

@ -86,23 +86,18 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
"Invalid line number field width: 0: Numerical result out of range", "Invalid line number field width: 0: Numerical result out of range",
)), )),
} }
match opts.get_one::<u64>(options::JOIN_BLANK_LINES) {
None => {}
Some(num) if *num > 0 => settings.join_blank_lines = *num,
Some(_) => errs.push(String::from(
"Invalid line number of blank lines: 0: Numerical result out of range",
)),
}
if let Some(num) = opts.get_one::<i64>(options::LINE_INCREMENT) { if let Some(num) = opts.get_one::<i64>(options::LINE_INCREMENT) {
settings.line_increment = *num; settings.line_increment = *num;
} }
if let Some(num) = opts.get_one::<i64>(options::STARTING_LINE_NUMBER) { if let Some(num) = opts.get_one::<i64>(options::STARTING_LINE_NUMBER) {
settings.starting_line_number = *num; settings.starting_line_number = *num;
} }
match opts.get_one::<String>(options::JOIN_BLANK_LINES) {
None => {}
Some(val) => {
let conv: Option<u64> = val.parse().ok();
match conv {
None => {
errs.push(String::from("Illegal value for -l"));
}
Some(num) => settings.join_blank_lines = num,
}
}
}
errs errs
} }

View file

@ -216,7 +216,8 @@ pub fn uu_app() -> Command {
.short('l') .short('l')
.long(options::JOIN_BLANK_LINES) .long(options::JOIN_BLANK_LINES)
.help("group of NUMBER empty lines counted as one") .help("group of NUMBER empty lines counted as one")
.value_name("NUMBER"), .value_name("NUMBER")
.value_parser(clap::value_parser!(u64)),
) )
.arg( .arg(
Arg::new(options::NUMBER_FORMAT) Arg::new(options::NUMBER_FORMAT)

View file

@ -1,4 +1,4 @@
// spell-checker:ignore iinvalid ninvalid vinvalid winvalid // spell-checker:ignore iinvalid linvalid ninvalid vinvalid winvalid
use crate::common::util::TestScenario; use crate::common::util::TestScenario;
#[test] #[test]
@ -244,3 +244,22 @@ fn test_invalid_line_increment() {
.stderr_contains("invalid value 'invalid'"); .stderr_contains("invalid value 'invalid'");
} }
} }
#[test]
fn test_join_blank_lines_zero() {
for arg in ["-l0", "--join-blank-lines=0"] {
new_ucmd!().arg(arg).fails().stderr_contains(
"Invalid line number of blank lines: 0: Numerical result out of range",
);
}
}
#[test]
fn test_invalid_join_blank_lines() {
for arg in ["-linvalid", "--join-blank-lines=invalid"] {
new_ucmd!()
.arg(arg)
.fails()
.stderr_contains("invalid value 'invalid'");
}
}