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

numfmt: prohibit --header=0 to align with GNU

Adjust header option handling to prohibit passing a value of 0 to align
with GNU numfmt. Also report header option parse errors as GNU does.

closes #1708
This commit is contained in:
Daniel Rocco 2021-02-14 18:25:28 -05:00 committed by Sylvestre Ledru
parent f8006f47df
commit f595164063
2 changed files with 42 additions and 12 deletions

View file

@ -291,11 +291,18 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
let header = match args.occurrences_of(options::HEADER) { let header = match args.occurrences_of(options::HEADER) {
0 => Ok(0), 0 => Ok(0),
_ => args _ => {
.value_of(options::HEADER) let value = args.value_of(options::HEADER).unwrap();
.unwrap()
.parse::<usize>() value
.map_err(|err| err.to_string()), .parse::<usize>()
.map_err(|_| value)
.and_then(|n| match n {
0 => Err(value),
_ => Ok(n),
})
.map_err(|value| format!("invalid header value {}", value))
}
}?; }?;
Ok(NumfmtOptions { Ok(NumfmtOptions {
@ -423,17 +430,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.arg(Arg::with_name(options::NUMBER).hidden(true).multiple(true)) .arg(Arg::with_name(options::NUMBER).hidden(true).multiple(true))
.get_matches_from(args); .get_matches_from(args);
let options = parse_options(&matches).unwrap(); let result =
parse_options(&matches).and_then(|options| match matches.values_of(options::NUMBER) {
let result = match matches.values_of(options::NUMBER) { Some(values) => handle_args(values, options),
Some(values) => handle_args(values, options), None => handle_stdin(options),
None => handle_stdin(options), });
};
match result { match result {
Err(e) => { Err(e) => {
show_info!("{}", e); show_info!("{}", e);
exit!(1); 1
} }
_ => 0, _ => 0,
} }

View file

@ -107,6 +107,30 @@ fn test_header_default() {
.stdout_is("header\n1000\n1100000\n100000000\n"); .stdout_is("header\n1000\n1100000\n100000000\n");
} }
#[test]
fn test_header_error_if_non_numeric() {
new_ucmd!()
.args(&["--header=two"])
.run()
.stderr_is("numfmt: invalid header value two");
}
#[test]
fn test_header_error_if_0() {
new_ucmd!()
.args(&["--header=0"])
.run()
.stderr_is("numfmt: invalid header value 0");
}
#[test]
fn test_header_error_if_negative() {
new_ucmd!()
.args(&["--header=-3"])
.run()
.stderr_is("numfmt: invalid header value -3");
}
#[test] #[test]
fn test_negative() { fn test_negative() {
new_ucmd!() new_ucmd!()