Skip to content

Commit

Permalink
Merge pull request #5103 from cakebaker/nl_join_blank_lines_show_erro…
Browse files Browse the repository at this point in the history
…r_if_zero

nl: show error if --join-blank-lines is zero
  • Loading branch information
sylvestre committed Jul 25, 2023
2 parents df74d8e + aef130d commit 9449eda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/uu/nl/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)),
}
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) {
settings.line_increment = *num;
}
if let Some(num) = opts.get_one::<i64>(options::STARTING_LINE_NUMBER) {
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
}
3 changes: 2 additions & 1 deletion src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ pub fn uu_app() -> Command {
.short('l')
.long(options::JOIN_BLANK_LINES)
.help("group of NUMBER empty lines counted as one")
.value_name("NUMBER"),
.value_name("NUMBER")
.value_parser(clap::value_parser!(u64)),
)
.arg(
Arg::new(options::NUMBER_FORMAT)
Expand Down
21 changes: 20 additions & 1 deletion tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// spell-checker:ignore iinvalid ninvalid vinvalid winvalid
// spell-checker:ignore iinvalid linvalid ninvalid vinvalid winvalid
use crate::common::util::TestScenario;

#[test]
Expand Down Expand Up @@ -256,3 +256,22 @@ fn test_invalid_line_increment() {
.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'");
}
}

0 comments on commit 9449eda

Please sign in to comment.