Skip to content

grep - -a/-I/--binary-files, -r/-d and the group-separator options ignore command-line order #177

Open

Description

@sylvestre

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:

  • -a always beats -I, which always beats --binary-files=TYPE;
  • -r/-R always beat -d ACTION;
  • --no-group-separator always 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: 0

GNU (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: 0

The 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

  1. added
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
  2. added 4 commits that reference this issue on Nov 25, 2026
    bc618a5
    8fcd2ee
    6dc6ee5
    8637414
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    gnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions