From b047c2a6defcb13efb8e695deda3a0d66b818297 Mon Sep 17 00:00:00 2001 From: arbelonson-source <269032023+arbelonson-source@users.noreply.github.com> Date: Mon, 31 Aug 2026 09:52:39 +0300 Subject: [PATCH] numfmt: accept hyphen-leading -d/--suffix/--padding values -d/--delimiter, --suffix, and --padding all had the same gap already fixed this session for several other options: their value was rejected as an unrecognized flag when given as its own argument (numfmt --padding -8, not the attached --padding=-8), since the Args were missing allow_hyphen_values. GNU's own numfmt accepts any argument as these options' values regardless of the first character -- including a negative --padding, which is meaningful (left-justifies instead of right-justifying). --padding already parsed as a signed isize, so no change was needed beyond letting clap route the value to it in the first place. AI-assisted-by: Claude Opus 5, via Claude Code --- src/uu/numfmt/src/numfmt.rs | 7 +++++-- tests/by-util/test_numfmt.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index ef7bc2cbde7..304727c2a7e 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -577,6 +577,7 @@ pub fn uu_app() -> Command { .long(DELIMITER) .value_name("X") .value_parser(ValueParser::os_string()) + .allow_hyphen_values(true) .help(translate!("numfmt-help-delimiter")), ) .arg( @@ -626,7 +627,8 @@ pub fn uu_app() -> Command { Arg::new(PADDING) .long(PADDING) .help(translate!("numfmt-help-padding")) - .value_name("N"), + .value_name("N") + .allow_hyphen_values(true), ) .arg( Arg::new(HEADER) @@ -656,7 +658,8 @@ pub fn uu_app() -> Command { Arg::new(SUFFIX) .long(SUFFIX) .help(translate!("numfmt-help-suffix")) - .value_name("SUFFIX"), + .value_name("SUFFIX") + .allow_hyphen_values(true), ) .arg( Arg::new(UNIT_SEPARATOR) diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 4db86944771..a17e634bd25 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -143,6 +143,41 @@ fn test_negative_padding() { .stdout_is("1000 \n1100000 \n100000000"); } +#[test] +fn test_negative_padding_as_separate_arg() { + // A negative padding value passed as its own argument (not attached + // with `=`) must not be mistaken for a new, unrecognized flag. + new_ucmd!() + .args(&["--from=si", "--padding", "-8"]) + .pipe_in("1K\n1.1M\n0.1G") + .succeeds() + .stdout_is("1000 \n1100000 \n100000000"); +} + +#[test] +fn test_suffix_hyphen_leading_as_separate_arg() { + // A hyphen-leading suffix value passed as its own argument (not + // attached with `=`) must not be mistaken for a new, unrecognized + // flag. + new_ucmd!() + .args(&["--suffix", "-x"]) + .pipe_in("5\n") + .succeeds() + .stdout_is("5-x\n"); +} + +#[test] +fn test_delimiter_hyphen_leading_as_separate_arg() { + // A hyphen-leading delimiter value passed as its own argument (not + // attached with `-d-x`/`=`) must not be mistaken for a new, + // unrecognized flag. + new_ucmd!() + .args(&["-d", "-x", "--field=1"]) + .pipe_in("5-x6") + .fails() + .stderr_contains("the delimiter must be a single character"); +} + #[test] fn test_header() { new_ucmd!()