fix(grep): support -v invert-match (#1477)#1528
Open
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Open
fix(grep): support -v invert-match (#1477)#1528ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Conversation
…on (rtk-ai#1477) The global `--verbose` count flag is registered with `global = true` and short `-v`, so `rtk grep -v PATTERN PATH` had `-v` consumed before clap ever reached the `Grep` subcommand handler. Result: invert intent was silently dropped and rtk returned matching lines instead of inverting. Two-part fix: 1. `preprocess_argv` runs before `Cli::try_parse_from` and rewrites a literal `-v` token into `--invert-match` only when the active subcommand is `grep`. Other subcommands keep `-v` as the verbose short alias. 2. `Commands::Grep` gains an explicit `--invert-match` boolean field. When set, the handler prepends `--invert-match` to `extra_args` so the flag reaches both the rg primary path and the grep fallback path (which now forwards `extra_args` instead of dropping them). The grep fallback in `grep_cmd::run` also previously dropped all `extra_args`, which would have broken the same flag for users without ripgrep installed. It now forwards them verbatim (minus `-r`/`--recursive`, which `grep -rn` already implies). Adds four parser-level regression tests covering the argv rewrite, the subcommand-scoping guard, and the end-to-end pipeline. Closes rtk-ai#1477 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Collaborator
📊 Automated PR Analysis
SummaryFixes a bug where Review Checklist
Linked issues: #1477 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
`rtk grep -v foo /etc/hosts` returned matching lines instead of inverting the search. The `-v` flag was silently dropped.
Root cause
The global `--verbose` count flag is registered with `global = true` and short `-v` in `Cli`. clap consumes globals before subcommand-specific positionals, so `-v` was eaten by verbose and never reached the `Grep` handler. `extra_args` ended up empty, so `rg` got no `-v`/`--invert-match` and returned matching lines.
A second latent issue: the grep fallback path (when `rg` is unavailable) was passing only `-rn pattern path` and discarding `extra_args` entirely. That would have broken the same flag for users without ripgrep installed.
Fix
`preprocess_argv` runs before `Cli::try_parse_from` and rewrites a literal `-v` token into `--invert-match` only when the active subcommand is `grep`. Every other subcommand keeps `-v` as the verbose short alias.
`Commands::Grep` gains an explicit `--invert-match` boolean field. When set, the handler prepends `--invert-match` to `extra_args` so the flag reaches both the rg primary path and the grep fallback path.
`grep_cmd::run` fallback now forwards `extra_args` to GNU grep instead of dropping them (skipping `-r`/`--recursive` since `grep -rn` already implies recursion).
Test plan
Closes #1477