Merge pull request #3686 from cakebaker/show_error_if_padding_is_zero

numfmt: show error if --padding is zero
This commit is contained in:
Sylvestre Ledru 2022-07-01 15:57:29 +02:00 committed by GitHub
commit 64bc20c2d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -103,7 +103,14 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
let transform = TransformOptions { from, to };
let padding = match args.value_of(options::PADDING) {
Some(s) => s.parse::<isize>().map_err(|err| err.to_string()),
Some(s) => s
.parse::<isize>()
.map_err(|_| s)
.and_then(|n| match n {
0 => Err(s),
_ => Ok(n),
})
.map_err(|s| format!("invalid padding value {}", s.quote())),
None => Ok(0),
}?;

View file

@ -593,3 +593,17 @@ fn test_invalid_argument_returns_status_1() {
.fails()
.code_is(1);
}
#[test]
fn test_invalid_padding_value() {
let padding_values = vec!["A", "0"];
for padding_value in padding_values {
new_ucmd!()
.arg(format!("--padding={}", padding_value))
.arg("5")
.fails()
.code_is(1)
.stderr_contains(format!("invalid padding value '{}'", padding_value));
}
}