Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Read + 'static>>;
Expand Down Expand Up @@ -283,6 +284,13 @@ fn extract_files(matches: &ArgMatches) -> UResult<Vec<OsString>> {
Some(Ok(x.clone()))
}
})
.chain(
matches
.get_many::<OsString>(options::FILES_AFTER_DASHES)
.into_iter()
.flatten()
.map(|x| Ok(x.clone())),
)
.collect();

if files.as_ref().is_ok_and(Vec::is_empty) {
Expand Down Expand Up @@ -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)]
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
Loading