From 863741454af7110c211fd97225e16cbd3aa06991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Arda=20G=C3=BCney?= Date: Tue, 24 Nov 2026 23:41:28 +0300 Subject: [PATCH] fix(cli): allow later options to override earlier ones in option groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Configure clap argument overrides for -a / -I / --binary-files, -r / -R / -d, and --group-separator / --no-group-separator so that the later option takes precedence over an earlier one as GNU grep does. Fixes #177 Signed-off-by: Yunus Arda Güney --- src/lib.rs | 32 ++++++++++++++++++++++---------- tests/test_grep.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7b70ff6..370bb1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,7 +237,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let context = matches.get_one::("context").copied(); let group_separator = matches .get_one::("group_separator") - .map_or("--", |s| s.as_str()); + .map(|s| s.as_str()); let no_group_separator = matches.get_flag("no_group_separator"); // Color output requires an explicit --color option. let color = matches @@ -427,7 +427,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { _ => true, } }; - let group_separator = (!no_group_separator).then_some(group_separator); + let group_separator = if no_group_separator { + None + } else { + Some(group_separator.unwrap_or("--")) + }; let use_color = match color { ColorMode::Always => true, ColorMode::Never => false, @@ -741,20 +745,23 @@ pub fn uu_app() -> Command { .long("binary-files") .value_name("TYPE") .help("assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'") - .value_parser(["binary", "text", "without-match"]), + .value_parser(["binary", "text", "without-match"]) + .overrides_with_all(["text", "skip_binary"]), ) .arg( Arg::new("text") .short('a') .long("text") .help("equivalent to --binary-files=text") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with_all(["binary_files", "skip_binary"]), ) .arg( Arg::new("skip_binary") .short('I') .help("equivalent to --binary-files=without-match") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with_all(["binary_files", "text"]), ) .arg( Arg::new("directories") @@ -762,7 +769,8 @@ pub fn uu_app() -> Command { .long("directories") .value_name("ACTION") .help("how to handle directories; ACTION is 'read', 'recurse', or 'skip'") - .value_parser(["read", "skip", "recurse"]), + .value_parser(["read", "skip", "recurse"]) + .overrides_with_all(["recursive", "dereference_recursive"]), ) .arg( Arg::new("devices") @@ -777,14 +785,16 @@ pub fn uu_app() -> Command { .short('r') .long("recursive") .help("like --directories=recurse") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with_all(["directories", "dereference_recursive"]), ) .arg( Arg::new("dereference_recursive") .short('R') .long("dereference-recursive") .help("likewise, but follow all symlinks") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with_all(["directories", "recursive"]), ) .arg( Arg::new("include") @@ -886,13 +896,15 @@ pub fn uu_app() -> Command { .value_name("SEP") .help("print SEP on line between matches with context") .default_value("--") - .allow_hyphen_values(true), + .allow_hyphen_values(true) + .overrides_with("no_group_separator"), ) .arg( Arg::new("no_group_separator") .long("no-group-separator") .help("do not print separator for matches with context") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with("group_separator"), ) .arg( Arg::new("color") diff --git a/tests/test_grep.rs b/tests/test_grep.rs index e2cbbeb..afeee86 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -2697,3 +2697,35 @@ fn diagnostics_follow_earlier_output() { "a.txt:foo a\ngrep: bin.dat: binary file matches\ngrep: missing.txt: No such file or directory\nb.txt:foo b\n" ); } + +#[test] +fn same_setting_options_last_wins() { + let (scene, mut c) = ucmd(); + scene.fixtures.write_bytes("bin", b"hit\0x\n"); + c.args(&["-a", "-I", "hit", "bin"]) + .fails_with_code(1) + .no_output(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["-I", "--binary-files=text", "hit", "bin"]) + .succeeds() + .stdout_only_bytes(b"hit\0x\n"); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--binary-files=without-match", "-a", "hit", "bin"]) + .succeeds() + .stdout_only_bytes(b"hit\0x\n"); + + let (scene, mut c) = ucmd(); + scene.fixtures.mkdir("tree"); + scene.fixtures.write("tree/f", "foo\n"); + c.args(&["-r", "-d", "skip", "foo", "tree"]) + .fails_with_code(1) + .no_output(); + + let (_s, mut c) = ucmd(); + c.args(&["-A0", "--no-group-separator", "--group-separator=::", "x"]) + .pipe_in("x\ny\nx\n") + .succeeds() + .stdout_only("x\n::\nx\n"); +}