Skip to content

grep: print GNU's message for an unknown option - #221

Open
VanshajPoonia wants to merge 1 commit into
uutils:mainfrom
VanshajPoonia:grep-unknown-option-214
Open

VanshajPoonia wants to merge 1 commit into
uutils:mainfrom
VanshajPoonia:grep-unknown-option-214

Conversation

@VanshajPoonia

Copy link
Copy Markdown

An unknown option printed clap's error block with a tip: and an unresolved usage line:

$ grep --frobnicate x </dev/null
error: unexpected argument '--frobnicate' found

  tip: to pass '--frobnicate' as a value, use '-- --frobnicate'

Usage: grep-usage

For more information, try '--help'.

Now it prints what GNU grep prints, still with exit 2:

$ grep --frobnicate x </dev/null
grep: unrecognized option '--frobnicate'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

A short option gives invalid option -- 'j' and also inside a cluster like -wj.
Other parse errors still go through uucore for now. I kept the change in grep as uucore's clap messages are shared with coreutils whose tests expect clap's wording.

New test unknown_option_is_reported_like_gnu (fails on main). cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.

Closes #214

@codspeed

codspeed Bot commented Nov 18, 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 VanshajPoonia:grep-unknown-option-214 (b4b7c45) 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 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.26%. Comparing base (bf6f25e) to head (b4b7c45).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #221      +/-   ##
==========================================
+ Coverage   97.22%   97.26%   +0.03%     
==========================================
  Files           6        6              
  Lines        2270     2301      +31     
  Branches      245      248       +3     
==========================================
+ Hits         2207     2238      +31     
  Misses         63       63              
Flag Coverage Δ
macOS_latest 98.15% <100.00%> (+0.02%) ⬆️
ubuntu_latest 98.15% <100.00%> (+0.02%) ⬆️
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.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for looking into this!
please check the --foo=bar case (inline comment)
and please squash the 3 commits into one

Comment thread src/lib.rs Outdated
}
let arg = err.get(clap::error::ContextKind::InvalidArg)?.to_string();
let problem = if arg.starts_with("--") {
format!("unrecognized option '{arg}'")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what about --foo=bar? GNU prints unrecognized option '--foo=bar', i think clap only gives --foo here
could you please check and add it to the test?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You were right, clap only gives --foo there. I now look up the full argument from argv, so --foo=bar prints unrecognized option '--foo=bar' like GNU. I added --no-such=1 to the test, and squashed everything into one commit.

@VanshajPoonia
VanshajPoonia force-pushed the grep-unknown-option-214 branch from d787ed1 to 640bb35 Compare November 18, 2026 23:37
Comment thread src/lib.rs Outdated
let given = args
.iter()
.filter_map(|a| a.to_str())
.find(|a| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this takes the first --foo=… anywhere in argv, so grep --foo -e --foo=x would name --foo=x
ok for me if we accept it, just checking it is known :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  • Thank you for pointing this out, and I fixed it instead of accepting it :D
  • It now names the argument clap actually rejected.
  • grep --foo -e --foo=x now says '--foo'.
  • I put both of the cases you told in test.

@VanshajPoonia
VanshajPoonia force-pushed the grep-unknown-option-214 branch 3 times, most recently from 17f1e24 to 2db97f9 Compare November 25, 2026 21:00

@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.

But why?

An option grep doesn't know printed clap's "error: unexpected argument"
block. Print GNU's three lines instead: "unrecognized option '--foo'"
for a long option, with its value when it has one, and "invalid option
-- 'j'" for a short one, then the usage and help lines. Exit with 2.
@VanshajPoonia
VanshajPoonia force-pushed the grep-unknown-option-214 branch from 2db97f9 to b4b7c45 Compare November 26, 2026 01:33
@VanshajPoonia

Copy link
Copy Markdown
Author

@sylvestre filed #214 for this as a GNU compat bug. GNU's filename-lineno.pl test runs grep --no-such-option and takes the text before the first : as the program name. With clap's message that is error, so every sub-test after it fails with exit 127. clap's message also prints an unfilled Usage: grep-usage line.

It does make grep's message differ from the clap-style one uucore prints for the other utils. If you'd rather fix this in uucore for every tool, I can move it there.

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: ...

3 participants