Skip to content

grep: stop parsing options at the first operand under POSIXLY_CORRECT - #238

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-posixly-correct
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-posixly-correct

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

With POSIXLY_CORRECT set (even to an empty value), GNU grep stops reading options at the first operand, so POSIXLY_CORRECT=1 grep foo t -i searches a file named -i. We always permuted options to the front and applied -i.

expand_num_shorthand already walks the arguments before clap sees them and knows which ones are operands, so under POSIXLY_CORRECT it now inserts -- in front of the first operand and stops there. A lone - counts as an operand. Trailing -NUM is left alone too. Without the variable nothing changes.

The test checks five argument lists with the variable set to 1 and to an empty string, comparing against GNU grep 3.11. It fails on main.

Closes #183

Comment thread src/lib.rs Outdated
// Narrow down to `-flags` (and `--options`) and strip the leading `-` for easier matching.
let Some(arg) = arg.strip_prefix(b"-") else {
// A lone `-` (standard input) is an operand too.
let Some(arg) = arg.strip_prefix(b"-").filter(|arg| !arg.is_empty()) else {

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.

Could've been a simpler && !arg.is_empty() here.

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.

Switched to && !arg.is_empty().

@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.23%. Comparing base (bf6f25e) to head (e24f4e6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #238      +/-   ##
==========================================
+ Coverage   97.22%   97.23%   +0.01%     
==========================================
  Files           6        6              
  Lines        2270     2281      +11     
  Branches      245      247       +2     
==========================================
+ Hits         2207     2218      +11     
  Misses         63       63              
Flag Coverage Δ
macOS_latest 98.14% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 98.14% <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

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-posixly-correct (e24f4e6) 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. ↩

Comment thread tests/test_grep.rs Outdated
.pipe_in("Foo\n")
.fails_with_code(2)
.stdout_is(*stdout)
.stderr_contains(format!("{missing}: "));

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.

could you please check the full No such file or directory message? -c: is a bit weak

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.

Updated to assert the full {missing}: No such file or directory error message.

Comment thread src/lib.rs Outdated
///
/// With `posixly_correct`, options end at the first operand, as POSIX requires:
/// a `--` is inserted in front of it and later arguments are left alone.
fn expand_num_shorthand(

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.

the function name doesn't really match anymore, no?

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.

Renamed to preprocess_args and updated doc comments accordingly.

@yunusarda-sudo
yunusarda-sudo force-pushed the grep-posixly-correct branch 2 times, most recently from e34b8bb to 5fdf1c3 Compare November 24, 2026 20:11
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 - POSIXLY_CORRECT is ignored: options after the first operand are still parsed

3 participants