fix(grep): rtk grep -v inverts match instead of bumping verbose (closes #1477)#2168
Open
tiagodocouto wants to merge 1 commit into
Open
fix(grep): rtk grep -v inverts match instead of bumping verbose (closes #1477)#2168tiagodocouto wants to merge 1 commit into
rtk grep -v inverts match instead of bumping verbose (closes #1477)#2168tiagodocouto wants to merge 1 commit into
Conversation
bf3135a to
576809f
Compare
`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
576809f to
17aa78e
Compare
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.
What
rtk grep -v foo filereturned matching lines instead of inverting them (#1477).verboseis a global flag with short-v, so clap consumed-vbefore thegrepsubcommand could see it. Only--invert-matchworked.Why not just a local
-vAdding
#[arg(short = 'v', long = "invert-match")]to theGrepsubcommand panics at startup — clap requires unique short names (verified against clap 4.6):Change (two parts)
--invert-matchflag (long-only) on the grep subcommand, forwarded to ripgrep. clap parses it in any position. (A bare--invert-matchbefore the positionals would otherwise be anUnknownArgument, sinceextra_argsistrailing_var_arg.)-vshorthand: a standalone-vtoken after thegrepsubcommand becomes--invert-matchbefore parsing. Scoped carefully:-vbeforegrep(rtk -v grep ...) stays verbose.--separator stops the rewrite, sortk grep -- -vsearches for the literal-v(GNU grep / getopt convention).--verboseremains available for a verbose grep.Verification
cargo build,cargo clippy --all-targets --all-features -- -D warnings,cargo fmt --all -- --check— all cleancargo test— full suite green; 8 new unit tests for the rewrite + parsertk grep -v apple file→ inverted + filtered;rtk grep apple file -v→ inverted;rtk grep apple file→ unchanged;rtk grep -- -v file→ literal searchCloses #1477