Skip to content

fix(grep): accept -n flag for grep/rg compatibility#135

Merged
aeppling merged 2 commits intortk-ai:masterfrom
heAdz0r:fix/grep-accept-n-flag
Feb 20, 2026
Merged

fix(grep): accept -n flag for grep/rg compatibility#135
aeppling merged 2 commits intortk-ai:masterfrom
heAdz0r:fix/grep-accept-n-flag

Conversation

@heAdz0r
Copy link
Contributor

@heAdz0r heAdz0r commented Feb 15, 2026

Summary

  • rtk grep -n "pattern" path fails with unexpected argument '-n' because clap rejects unknown flags before positional arguments
  • Users naturally place -n before the pattern (muscle memory from grep -n / rg -n)
  • The flag is a no-op — grep_cmd::run() already passes -n to ripgrep unconditionally (line 24) — but clap must accept it

Reproduction

rtk grep -n "TODO" src/
# error: unexpected argument '-n' found
#   tip: to pass '-n' as a value, use '-- -n'

Fix

Adds -n/--line-numbers as an explicit bool field to the Grep command enum variant, ignored in the match arm (line_numbers: _). Same pattern as Read command which already defines -n (main.rs:105).

Files Changed

File Changes Purpose
src/main.rs +29 Add -n flag to Grep, ignore in dispatch, add 2 clap parsing tests

Test plan

  • cargo fmt --all --check — clean (no formatting changes in main.rs)
  • cargo clippy --all-targets — 0 errors
  • cargo test — 340 passed
  • Manual: cargo run -- grep -n "fn run" src/grep_cmd.rs — works

🤖 Generated with Claude Code

`rtk grep -n "pattern" path` failed with "unexpected argument '-n'"
because clap didn't recognize -n before the positional arguments.

Users naturally place -n before the pattern (muscle memory from
grep/rg). The flag is a no-op since grep_cmd::run() already passes
-n to ripgrep unconditionally, but clap must accept it.

Adds -n/--line-numbers as an explicit bool field to the Grep command
and ignores it in the match arm. Test added in grep_cmd::tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pszymkowiak
Copy link
Collaborator

Thanks for the fix — the approach is clean and correct. Same pattern as the -n flag on the Read command.

The PR has merge conflicts with master (main.rs has changed a lot since Feb 15). Could you rebase on master and force-push? Once the conflicts are resolved, this is good to merge.

@aeppling aeppling merged commit 7d561cc into rtk-ai:master Feb 20, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments