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