Repository navigation
Description
Known: #2 (merged PR) — #2; #30 (merged PR) — #30
In GNU grep these options set the same setting, so the one given last wins: -a is
documented as "equivalent to --binary-files=text", -I as "equivalent to
--binary-files=without-match", -r as "like --directories=recurse", and
--no-group-separator / --group-separator=SEP both set the separator. uu_grep resolves
each group with a fixed priority instead:
-aalways beats-I, which always beats--binary-files=TYPE;-r/-Ralways beat-d ACTION;--no-group-separatoralways beats--group-separator=SEP.
So a later option can be silently overridden by an earlier one, changing output and exit
status.
Reproduction
Rust (incorrect)
$ printf 'hit\0x\n' > bin
$ ./target/release/grep -a -I hit bin | cat -v
hit^@x
# Exit code: 0
$ ./target/release/grep -I --binary-files=text hit bin | cat -v
# Exit code: 1
$ mkdir dir; echo foo > dir/f
$ ./target/release/grep -r -d skip foo dir
dir/f:foo
# Exit code: 0
$ printf 'x\ny\nx\n' | ./target/release/grep -A0 --no-group-separator --group-separator=XX x
x
x
# Exit code: 0GNU (correct)
$ LC_ALL=C /usr/bin/grep -a -I hit bin | cat -v
# Exit code: 1
$ LC_ALL=C /usr/bin/grep -I --binary-files=text hit bin | cat -v
hit^@x
# Exit code: 0
$ LC_ALL=C /usr/bin/grep -r -d skip foo dir
# Exit code: 1
$ printf 'x\ny\nx\n' | LC_ALL=C /usr/bin/grep -A0 --no-group-separator --group-separator=XX x
x
XX
x
# Exit code: 0The reverse orders (-I -a, -d skip -r, --group-separator=XX --no-group-separator)
already agree with GNU.
Root cause
uumain combines the independent clap args with fixed precedence, e.g. src/lib.rs:307-318:
let binary_mode = if text {
BinaryMode::Text
} else if skip_binary {
BinaryMode::WithoutMatch
} else {
match binary_files {and likewise directory_mode (src/lib.rs:298-306, recursive || dereference_recursive
checked first) and group_separator (src/lib.rs:391,
(!no_group_separator).then_some(group_separator)).
Test
#[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 (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");
}Reported by Anthropic's OSS Scanner (ANT-2026-39HMXLZG).
Activity
fix(cli): allow later options to override earlier ones in option groups
fix(cli): allow later options to override earlier ones in option groups
fix(cli): allow later options to override earlier ones in option groups
fix(cli): allow later options to override earlier ones in option groups