Skip to content

grep - abbreviated long options (--cou, --reg=, --ignore) are rejected #156

Done

Description

@sylvestre

GNU grep (getopt_long) accepts any unambiguous prefix of a long option: --cou for
--count, --reg=foo for --regexp=foo, --max 1 for --max-count 1, --ignore for
--ignore-case. An ambiguous prefix (--co) is an error. uu_grep only accepts the exact
names, so every abbreviation fails with unexpected argument and exit 2, and scripts or
habits that rely on GNU's abbreviations break.

Reproduction

Rust (incorrect)

$ printf 'a\nb\n' | ./target/release/grep --cou a
error: unexpected argument '--cou' found

tip: a similar argument exists: '--count'

Usage: grep-usage

For more information, try '--help'.
# Exit code: 2

$ printf 'a\nb\n' | ./target/release/grep --reg=a
error: unexpected argument '--reg' found

tip: a similar argument exists: '--regexp'
...
# Exit code: 2

GNU (correct)

$ printf 'a\nb\n' | LC_ALL=C /usr/bin/grep --cou a
1
# Exit code: 0

$ printf 'a\nb\n' | LC_ALL=C /usr/bin/grep --reg=a
a
# Exit code: 0

$ printf 'a\nb\n' | LC_ALL=C /usr/bin/grep --co a
/usr/bin/grep: option '--co' is ambiguous; possibilities: '--context' '--color' '--colour' '--count'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
# Exit code: 2

Root cause

The Command built in uu_app() (src/lib.rs:493-502) never enables clap's long-option
inference:

Command::new("grep")
    .version(env!("CARGO_PKG_VERSION"))
    .about("Search for PATTERNS in each FILE.")
    .disable_help_flag(true)
    .disable_version_flag(true)
    ...
    .args_override_self(true)

Test

#[test]
fn abbreviated_long_options() {
    let (_s, mut c) = ucmd();
    c.args(&["--cou", "a"]).pipe_in("a\nb\na\n").succeeds().stdout_only("2\n");

    let (_s, mut c) = ucmd();
    c.args(&["--reg=b"]).pipe_in("a\nb\n").succeeds().stdout_only("b\n");

    let (_s, mut c) = ucmd();
    c.args(&["--max", "1", "a"]).pipe_in("a\na\n").succeeds().stdout_only("a\n");

    let (_s, mut c) = ucmd();
    c.args(&["--reg", "-7"]).pipe_in("-7\nx\n").succeeds().stdout_only("-7\n");

    // Ambiguous prefix stays a usage error.
    let (_s, mut c) = ucmd();
    c.args(&["--co", "a"]).pipe_in("a\n").fails_with_code(2);
}

Reported by Anthropic's OSS Scanner (ANT-2026-MEA9YMEZ).

Activity

  1. added
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
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