Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -216,7 +216,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 @@ -244,3 +244,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'");
}
}