From 4f64372dd6ac6086bc38c01a2d56cabf6adf42cc Mon Sep 17 00:00:00 2001 From: arbelonson-source <269032023+arbelonson-source@users.noreply.github.com> Date: Mon, 31 Aug 2026 09:54:47 +0300 Subject: [PATCH] stat: accept a hyphen-leading --format/--printf/-c value --format (-c) and --printf's value was rejected as an unrecognized flag when given as its own argument (stat --format -%n f, not the attached -c-%n or --format=-%n): the Args were missing allow_hyphen_values, the same gap already fixed this session for several other options. GNU's own stat accepts any argument as the format string regardless of its first character. AI-assisted-by: Claude Opus 5, via Claude Code --- src/uu/stat/src/stat.rs | 4 +++- tests/by-util/test_stat.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 3dd2b421d70..45817649c73 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -1540,12 +1540,14 @@ pub fn uu_app() -> Command { .short('c') .long(options::FORMAT) .help(translate!("stat-help-format")) - .value_name("FORMAT"), + .value_name("FORMAT") + .allow_hyphen_values(true), ) .arg( Arg::new(options::PRINTF) .long(options::PRINTF) .value_name("FORMAT") + .allow_hyphen_values(true) .help(translate!("stat-help-printf")), ) .arg( diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index b64b850a6aa..ab51bcab5c7 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -25,6 +25,25 @@ fn test_invalid_option() { new_ucmd!().arg("-w").arg("-q").arg("/").fails(); } +#[test] +fn test_format_hyphen_leading_as_separate_arg() { + // A hyphen-leading format string passed as its own argument (not + // attached with `=`) must not be mistaken for a new, unrecognized + // flag. + new_ucmd!() + .args(&["--format", "-%n", "/"]) + .succeeds() + .stdout_is("-/\n"); + new_ucmd!() + .args(&["--printf", "-%n", "/"]) + .succeeds() + .stdout_is("-/"); + new_ucmd!() + .args(&["-c", "-%n", "/"]) + .succeeds() + .stdout_is("-/\n"); +} + #[cfg(unix)] const NORMAL_FORMAT_STR: &str = "%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s %u %U %x %X %y %Y %z %Z"; // avoid "%w %W" (birth/creation) due to `stat` limitations and linux kernel & rust version capability variations