Skip to content

grep: accept unambiguous abbreviations of long options - #239

Merged
sylvestre merged 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-abbrev-long-options
Nov 25, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-abbrev-long-options

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

GNU grep accepts any unambiguous prefix of a long option (--cou for --count, --reg=x, --max 1); an ambiguous one such as --co is a usage error. We only accepted the full names.

uu_app() now sets clap's infer_long_args. The -NUM pre-pass also has to know when an abbreviated option takes the next argument as its value, otherwise --reg -7 would turn -7 into -C 7. Instead of keeping a second hand-written list of value options, it now asks the Command for the option, using the same rule as clap: an exact name wins, otherwise the prefix must pick a single option. This keeps --binary -1 as the --binary flag followed by -1.

The new test fails on main and checks the cases from the issue plus --binary-f, --colo= and the exact-name case against GNU grep 3.11.

Closes #156

Comment thread src/lib.rs
.get_arguments()
.filter(|arg| names(arg).any(|n| n.starts_with(name)));
let arg = match (exact, prefixed.next(), prefixed.next()) {
(Some(arg), _, _) | (None, Some(arg), None) => arg,

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.

That seems a little convoluted to me...

And in what way does this get_arguments() logic produce values that are different from LONG_OPTS_WITH_VALUE?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because LONG_OPTS_WITH_VALUE only held full, exact option names (e.g. regexp). When an unambiguous abbreviation like --reg -7 is passed, infer_long_args(true) in clap matches it to regexp, but the shorthand pre-pass did not recognize --reg in LONG_OPTS_WITH_VALUE and erroneously expanded -7 to -C 7. Reading from Command lets the pre-pass match the exact prefix resolution that clap will perform.

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.

??? Are you saying it was impossible to implement a prefix test? :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not impossible, I overcomplicated it. Done in #259.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.15%. Comparing base (83db14b) to head (a9b4881).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #239      +/-   ##
==========================================
+ Coverage   97.12%   97.15%   +0.02%     
==========================================
  Files           6        6              
  Lines        2227     2249      +22     
  Branches      241      243       +2     
==========================================
+ Hits         2163     2185      +22     
  Misses         64       64              
Flag Coverage Δ
macOS_latest 98.07% <100.00%> (+0.01%) ⬆️
ubuntu_latest 98.07% <100.00%> (+0.01%) ⬆️
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.

@codspeed

codspeed Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

Warning

uutils has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

1 warning

⚠️ 1 benchmark is 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-abbrev-long-options (a9b4881) with main (83db14b)

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

Comment thread src/lib.rs Outdated
}

// Only built when a long option shows up.
let cmd = std::cell::LazyCell::new(uu_app);

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.

uu_app() is now built twice, here and in uumain
could we build it once in uumain and pass it to expand_num_shorthand?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, updated! uu_app() is now built once in uumain and passed as &app to expand_num_shorthand.

@yunusarda-sudo
yunusarda-sudo force-pushed the grep-abbrev-long-options branch from 49087e4 to a9b4881 Compare November 24, 2026 19:59
@sylvestre
sylvestre merged commit bf6f25e into uutils:main Nov 25, 2026
25 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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 - abbreviated long options (--cou, --reg=, --ignore) are rejected

3 participants