Skip to content

fix(grep): rtk grep -v inverts match instead of bumping verbose (closes #1477)#2168

Open
tiagodocouto wants to merge 1 commit into
rtk-ai:developfrom
tiagodocouto:fix/grep-invert-v
Open

fix(grep): rtk grep -v inverts match instead of bumping verbose (closes #1477)#2168
tiagodocouto wants to merge 1 commit into
rtk-ai:developfrom
tiagodocouto:fix/grep-invert-v

Conversation

@tiagodocouto
Copy link
Copy Markdown

@tiagodocouto tiagodocouto commented May 30, 2026

What

rtk grep -v foo file returned matching lines instead of inverting them (#1477). verbose is a global flag with short -v, so clap consumed -v before the grep subcommand could see it. Only --invert-match worked.

Why not just a local -v

Adding #[arg(short = 'v', long = "invert-match")] to the Grep subcommand panics at startup — clap requires unique short names (verified against clap 4.6):

Command grep: Short option names must be unique for each argument,
but '-v' is in use by both 'invert_match' and 'verbose'

Change (two parts)

  1. Native --invert-match flag (long-only) on the grep subcommand, forwarded to ripgrep. clap parses it in any position. (A bare --invert-match before the positionals would otherwise be an UnknownArgument, since extra_args is trailing_var_arg.)
  2. argv rewrite for the -v shorthand: a standalone -v token after the grep subcommand becomes --invert-match before parsing. Scoped carefully:
    • -v before grep (rtk -v grep ...) stays verbose.
    • a -- separator stops the rewrite, so rtk grep -- -v searches for the literal -v (GNU grep / getopt convention).
    • --verbose remains available for a verbose grep.

Verification

  • cargo build, cargo clippy --all-targets --all-features -- -D warnings, cargo fmt --all -- --check — all clean
  • cargo test — full suite green; 8 new unit tests for the rewrite + parse
  • smoke (real binary, rtk filter path — not the raw-grep fallback): rtk grep -v apple file → inverted + filtered; rtk grep apple file -v → inverted; rtk grep apple file → unchanged; rtk grep -- -v file → literal search

Closes #1477

@tiagodocouto tiagodocouto force-pushed the fix/grep-invert-v branch 3 times, most recently from bf3135a to 576809f Compare May 30, 2026 11:08
`verbose` is a global flag with short `-v`, so clap consumed `-v` before
the `grep` subcommand could see it — `rtk grep -v foo file` returned
matching lines instead of inverting them (rtk-ai#1477). Only `--invert-match`
worked.

A subcommand-local `-v` is impossible: clap rejects two args sharing a
short name (`'-v' is in use by both 'invert_match' and 'verbose'`). The
fix has two parts:

1. Add a native long-only `--invert-match` flag to the grep subcommand,
   forwarded to ripgrep. clap parses it in any position. (A bare
   `--invert-match` before the positionals would otherwise be an
   UnknownArgument, since extra_args is trailing_var_arg — which forced a
   fallback to raw grep, bypassing rtk's filter.)
2. Rewrite a standalone `-v` token after the `grep` subcommand into
   `--invert-match` before parsing, to restore the `-v` shorthand. A `-v`
   preceding `grep` still means verbose; `--verbose` still works for a
   verbose grep; a `--` separator stops the rewrite so `rtk grep -- -v`
   keeps the literal pattern (GNU grep / getopt convention).

8 unit tests cover the rewrite (leading/trailing `-v`, verbose-before-grep,
other-commands-untouched, `--` separator, exact-token-only) and the parse
(rewritten `-v` and native leading `--invert-match` both set the flag).

Closes rtk-ai#1477
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.

rtk grep "-v" does not invert the search

1 participant