Skip to content

fix(grep): support -v invert-match (#1477)#1528

Open
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes:fix/issue-1477
Open

fix(grep): support -v invert-match (#1477)#1528
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes:fix/issue-1477

Conversation

@ousamabenyounes
Copy link
Copy Markdown
Contributor

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

  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`. Every other subcommand keeps `-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.

  3. `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

  • `test_preprocess_grep_v_becomes_invert_match` — argv rewrite happens.
  • `test_preprocess_only_rewrites_for_grep_subcommand` — `rtk git -v status` keeps `-v` as verbose.
  • `test_grep_invert_match_flag_parses` — clap accepts `--invert-match`.
  • `test_grep_dash_v_full_pipeline_inverts_search` — full argv → preprocess → clap pipeline lands on `Grep::invert_match=true` with `cli.verbose==0`.
  • `cargo test --all` → 1685 passed, 0 failed.
  • Manually verified post-fix on `/etc/hosts`:
    • `rtk grep -v localhost /etc/hosts` → 13 lines (all non-localhost lines, including comments)
    • `rtk grep localhost /etc/hosts` → 2 lines (still inverts correctly when -v absent)
    • `rtk grep --invert-match localhost /etc/hosts` → 13 lines (long form also works)

Closes #1477

…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>
@pszymkowiak pszymkowiak added bug Something isn't working effort-medium 1-2 jours, quelques fichiers labels Apr 25, 2026
@pszymkowiak
Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes a bug where rtk grep -v had the -v flag silently consumed by the global --verbose flag instead of being passed as --invert-match to the grep/rg backend. Adds a pre-clap argv preprocessor that rewrites -v to --invert-match only for the grep subcommand, adds an explicit --invert-match field to the Grep command, and fixes the GNU grep fallback to forward extra_args.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1477


Analyzed automatically by wshm · This is an automated analysis, not a human review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-medium 1-2 jours, quelques fichiers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants