fix(grep): honor -c count flag (#1452)#1529
Open
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Open
fix(grep): honor -c count flag (#1452)#1529ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Conversation
`rtk grep -c PATTERN PATH` returned the regular match summary because clap was parsing the bare `-c` token as the short alias of `context_only`, not the new count flag. The bug consumed the pattern positional and silently ignored the user's intent. Fix is two parts: 1. Add a `--count` flag on the Grep subcommand and route it to a new `run_count` helper in `grep_cmd.rs`. The helper invokes `rg --count` (with a GNU `grep -c` fallback), then mirrors plain `grep -c` semantics: a single regular file emits the bare integer, a directory or multi-file path emits `path:N` per file. Tracking still records the full output. 2. Add a `preprocess_argv` argv rewrite in `main.rs` that translates the literal `-c` token into `--count` when (and only when) the `grep` subcommand is in play. This is the minimal escape hatch for the short-alias collision without touching unrelated subcommands. Regression tests cover both the argv rewrite (grep vs git) and the `count` flag reaching the handler with `context_only` left untouched. Closes rtk-ai#1452 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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 (#1452)
rtk grep -c PATTERN PATHignored the-cflag and returned the regular match summary + context lines instead of a line count. Plaingrep -candrg --countemit a single integer (orpath:Nper file) — the existing rtk behaviour broke any script doingn=$(rtk grep -c …).Root cause
The Grep subcommand has
context_only: boolwhose short alias is-c. Clap consumed the bare-ctoken before the newcountflag could be parsed, silently flippingcontext_onlyon and dropping the pattern positional.Fix
--countflag onGrep(src/main.rs) routed to a dedicatedrun_countpath ingrep_cmd.rs.run_counthelper invokesrg --count(with a GNUgrep -cfallback) and mirrors plaingrep -csemantics: bare integer for a single regular file,path:Nper file for directory / multi-file. Tracking still records the command.preprocess_argvargv rewrite (src/main.rs) translates the literal-ctoken into--countonly when thegrepsubcommand is in play. Other subcommands (e.g.git -c user.name=…) are untouched. This is the minimal escape hatch for the short-alias collision; no other rtk subcommand currently needs it.Test plan
cargo fmt --allcargo test --all— 1683 passed, 0 failedsrc/main.rs:test_preprocess_argv_grep_dash_c_rewritten—rtk grep -c TODO src/parses withcount=true,context_only=falsetest_preprocess_argv_non_grep_dash_c_untouched—rtk git -c user.name=Foo logis left alonertk grep -c foo /tmp/t.txt→3(matches plaingrep -c)rtk grep -c TODO src/cmds/system/→path:Nper file (matchesgrep -rc)Closes #1452
🤖 Generated with Claude Code