mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #5065 from cakebaker/nl_number_width_zero
nl: show error if --number-width is zero
This commit is contained in:
commit
a1b4d25d0e
3 changed files with 42 additions and 12 deletions
|
@ -94,17 +94,12 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match opts.get_one::<String>(options::NUMBER_WIDTH) {
|
match opts.get_one::<usize>(options::NUMBER_WIDTH) {
|
||||||
None => {}
|
None => {}
|
||||||
Some(val) => {
|
Some(num) if *num > 0 => settings.number_width = *num,
|
||||||
let conv: Option<usize> = val.parse().ok();
|
Some(_) => errs.push(String::from(
|
||||||
match conv {
|
"Invalid line number field width: ‘0’: Numerical result out of range",
|
||||||
None => {
|
)),
|
||||||
errs.push(String::from("Illegal value for -w"));
|
|
||||||
}
|
|
||||||
Some(num) => settings.number_width = num,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
match opts.get_one::<String>(options::STARTING_LINE_NUMBER) {
|
match opts.get_one::<String>(options::STARTING_LINE_NUMBER) {
|
||||||
None => {}
|
None => {}
|
||||||
|
|
|
@ -251,7 +251,8 @@ pub fn uu_app() -> Command {
|
||||||
.short('w')
|
.short('w')
|
||||||
.long(options::NUMBER_WIDTH)
|
.long(options::NUMBER_WIDTH)
|
||||||
.help("use NUMBER columns for line numbers")
|
.help("use NUMBER columns for line numbers")
|
||||||
.value_name("NUMBER"),
|
.value_name("NUMBER")
|
||||||
|
.value_parser(clap::value_parser!(usize)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// spell-checker:ignore ninvalid
|
// spell-checker:ignore ninvalid winvalid
|
||||||
use crate::common::util::TestScenario;
|
use crate::common::util::TestScenario;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -122,3 +122,37 @@ fn test_invalid_number_format() {
|
||||||
.stderr_contains("invalid value 'invalid'");
|
.stderr_contains("invalid value 'invalid'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_number_width() {
|
||||||
|
for width in 1..10 {
|
||||||
|
for arg in [format!("-w{width}"), format!("--number-width={width}")] {
|
||||||
|
let spaces = " ".repeat(width - 1);
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(arg)
|
||||||
|
.pipe_in("test")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is(format!("{spaces}1\ttest\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_number_width_zero() {
|
||||||
|
for arg in ["-w0", "--number-width=0"] {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(arg)
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("Invalid line number field width: ‘0’: Numerical result out of range");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_number_width() {
|
||||||
|
for arg in ["-winvalid", "--number-width=invalid"] {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(arg)
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("invalid value 'invalid'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue