Skip to content

grep: format unknown option errors to match GNU grep - #226

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-unknown-option-diagnostic
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-unknown-option-diagnostic

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Fixes #214

Problem

For an unknown option, GNU grep prints one line prefixed with the program name (grep: unrecognized option '--frobnicate' or grep: invalid option -- 'j'), followed by:

Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

with exit status 2.

Previously, uu_grep routed unknown arguments to handle_clap_result_with_exit_code, which output clap's multi-line block starting with error: unexpected argument ... found, a tip:, and the unresolved placeholder Usage: grep-usage. This breaks tests like GNU grep's filename-lineno.pl that parse the program prefix from stderr.

Solution

  • Handle clap::error::ErrorKind::UnknownArgument in argument parsing, extracting ContextKind::InvalidArg to detect whether the option was short (-<opt>) or long (--<opt>).
  • Format the diagnostic as unrecognized option '--<opt>' or invalid option -- '<opt>' followed by the standard usage and help pointers.
  • Preserve clap's standard help/version handling and fall back to clap's error formatter for other error kinds.
  • Add regression test unknown_option_diagnostic in tests/test_grep.rs.

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing yunusarda-sudo:grep-unknown-option-diagnostic (5e0782f) with main (bf6f25e)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.23077% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.90%. Comparing base (bf6f25e) to head (5e0782f).

Files with missing lines Patch % Lines
src/lib.rs 69.23% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #226      +/-   ##
==========================================
- Coverage   97.22%   96.90%   -0.32%     
==========================================
  Files           6        6              
  Lines        2270     2295      +25     
  Branches      245      250       +5     
==========================================
+ Hits         2207     2224      +17     
- Misses         63       71       +8     
Flag Coverage Δ
macOS_latest 97.80% <69.23%> (-0.34%) ⬇️
ubuntu_latest 97.80% <69.23%> (-0.34%) ⬇️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yunusarda-sudo
yunusarda-sudo force-pushed the grep-unknown-option-diagnostic branch 5 times, most recently from 6548b28 to 7292357 Compare November 24, 2026 19:50
@yunusarda-sudo
yunusarda-sudo force-pushed the grep-unknown-option-diagnostic branch from 7292357 to 5e0782f Compare November 25, 2026 22:22

@lhecker lhecker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with #221: But why?

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

The motivation comes from issue #214 opened by @sylvestre: the GNU grep test suite script filename-lineno.pl derives the binary name by executing grep --no-such-option and capturing the prefix before the first :. When clap prints error: unexpected argument..., the GNU test suite extracts error as the program name and subsequently fails all sub-tests with exit 127 (error: command not found).

Additionally, clap was printing the unresolved placeholder Usage: grep-usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - an unknown option prints clap's error: ... block (and Usage: grep-usage) instead of grep: ...

2 participants