From 1e4d0c0344e4ef8b177eab86fd95b05b5f8ac3d9 Mon Sep 17 00:00:00 2001 From: Parman Mohammadalizadeh Date: Sun, 25 Oct 2026 19:21:04 +0100 Subject: [PATCH] fmt: treat -N operands after -- as file names --- src/uu/fmt/src/fmt.rs | 15 +++++++++++++++ tests/by-util/test_fmt.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index f5d7d1dc544..c904b89ce9a 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -71,6 +71,7 @@ mod options { pub const QUICK: &str = "quick"; pub const TAB_WIDTH: &str = "tab-width"; pub const FILES_OR_WIDTH: &str = "files"; + pub const FILES_AFTER_DASHES: &str = "files-after-dashes"; } pub type FileOrStdReader = BufReader>; @@ -283,6 +284,13 @@ fn extract_files(matches: &ArgMatches) -> UResult> { Some(Ok(x.clone())) } }) + .chain( + matches + .get_many::(options::FILES_AFTER_DASHES) + .into_iter() + .flatten() + .map(|x| Ok(x.clone())), + ) .collect(); if files.as_ref().is_ok_and(Vec::is_empty) { @@ -501,6 +509,13 @@ pub fn uu_app() -> Command { .value_parser(clap::value_parser!(OsString)) .allow_negative_numbers(true), ) + .arg( + Arg::new(options::FILES_AFTER_DASHES) + .action(ArgAction::Append) + .value_parser(clap::value_parser!(OsString)) + .last(true) + .hide(true), + ) } #[cfg(test)] diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index 08974a60b80..f72fa3f078f 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -152,6 +152,33 @@ fn test_fmt_positional_width_not_first() { .stderr_contains("fmt: invalid option -- 1; -WIDTH is recognized only when it is the first\noption; use -w N instead"); } +#[test] +fn test_fmt_negative_number_file_after_double_dash() { + use uutests::util::TestScenario; + use uutests::util_name; + + let scene = TestScenario::new(util_name!()); + scene.fixtures.write("-9", "hidden text\n"); + scene.fixtures.write("f", "the quick brown fox jumps\n"); + for args in [&["--", "-9", "f"][..], &["-u", "--", "-9", "f"]] { + scene + .ucmd() + .args(args) + .succeeds() + .stdout_only("hidden text\nthe quick brown fox jumps\n"); + } + scene + .ucmd() + .args(&["f", "--", "-9"]) + .succeeds() + .stdout_only("the quick brown fox jumps\nhidden text\n"); + scene + .ucmd() + .args(&["--", "-9"]) + .succeeds() + .stdout_only("hidden text\n"); +} + #[test] fn test_fmt_width_not_valid_number() { new_ucmd!()