Skip to content

fix(grep): disable help flag to allow -h passthrough - #2532

Closed
axelray-dev wants to merge 1 commit into
rtk-ai:developfrom
axelray-dev:fix/grep-disable-help-flag-2391
Closed

axelray-dev wants to merge 1 commit into
rtk-ai:developfrom
axelray-dev:fix/grep-disable-help-flag-2391

Conversation

@axelray-dev

Copy link
Copy Markdown
Contributor

Fixes #2391.

Add #[command(disable_help_flag = true)] to the Grep variant so clap does not intercept -h as --help before trailing_var_arg can collect it.

Follows the pattern of merged PR #650 (psql disable_help_flag).

Problem

rtk grep -h PATTERN FILE prints rtk's help text and exits 0 instead of forwarding -h to grep/rg. Clap's auto-generated help flag intercepts -h before the trailing var-arg collection runs.

Fix

One-line addition to src/main.rs:

/// Compact grep - strips whitespace, truncates, groups by file
#[command(disable_help_flag = true)]
Grep {

Side effect

rtk grep --help forwards to rg/grep help instead of rtk's usage text. This matches the psql behavior.

Tests

  • rtk grep -h alpha a.txt b.txt shows search matches, not help text
  • rtk grep --help forwards to underlying tool help
  • rtk grep -v pattern . still works
  • cargo test passes

Complementary to PR #2334 (rewrite-layer passthrough). Both fixes can coexist.

DCO: Signed-off.

Add #[command(disable_help_flag = true)] to the Grep variant so clap
does not intercept -h as --help before trailing_var_arg can collect it.

Follows the pattern of merged PR rtk-ai#650 (psql disable_help_flag).

Fixes rtk-ai#2391

Signed-off-by: axelray-dev <[email protected]>
@axelray-dev
axelray-dev marked this pull request as ready for review June 21, 2026 18:07
devnulled added a commit to devnulled/rtk that referenced this pull request Aug 30, 2026
rtk's own tuning options on `Commands::Grep` declared short forms that
collide with native grep/rg flags of the same letter. This removes the two
that remain, and deletes one option outright.

`--max-len` loses `-l`. `-l` is grep's --files-with-matches, and the old
binding split behavior on whether the PATTERN parsed as a usize:

  - Non-numeric: clap rejected it, run_fallback re-ran raw grep, and the
    user got the right answer. Wasteful, not harmful.
  - Numeric: clap ACCEPTED it, and the result was silently wrong.

        # hit.txt contains "listen on port 8080 today"; miss.txt does not
        $ rtk grep -l 8080 hit.txt miss.txt
        before:  (no output)   exit=1     <- claims nothing matched
        after:   hit.txt       exit=0

    `-l 8080` set max_len=8080, leaving hit.txt as the pattern and miss.txt
    as the only path. With a single file the filename becomes the pattern,
    no path is left, and rtk falls back to stdin: exit 1 against /dev/null,
    hangs on a terminal. Port numbers, years and error codes are ordinary
    patterns, and the Claude Code hook rewrites plain `grep` into `rtk grep`
    transparently -- so this reached users who never opted in.

    Note this is not a savings story. `-l` is a has_format_flag passthrough
    on the fixed path too (search.rs:548), so there is no compaction for
    this flag either way. The bug was a wrong answer.

`--file-type` is removed entirely, with its `-t` short, rather than left as
a parse-only no-op. It was destructured as `file_type: _` at the call site
and never reached search::run, so it parsed and then did nothing.
`rtk rg -t rust` is unaffected: Rg has no such field and forwards `-t` to
real ripgrep.

Deliberately typed `fix`, not `fix!`, even though a documented long option
disappears. The option was inert -- it never filtered anything -- so no
working behavior is lost, and rtk-ai#3259 set the precedent for removing a
colliding grep short under plain `fix`. Recording the choice here because
silence would read as an oversight.

User-visible consequence: `rtk grep -t rust` goes from silently returning
unfiltered results to surfacing the engine's own error. BSD/macOS grep
prints `invalid option -- t`; GNU grep prints `invalid option -- 't'`.

`--max`/`-m` is NOT part of this change. rtk-ai#3259 removed that short already;
this branch is rebased onto it and preserves its rationale comment verbatim.

The struct NOTE deliberately does not claim to finish the job. clap's auto
`-h` still shadows grep/rg's --no-filename (that is rtk-ai#2532's scope), and
removing a clap short does not make a letter safe end to end: search.rs
shares one VALUE_FLAGS_SHORT table across both engines, so a letter taking
a value in rg but not grep still eats the next token inside a cluster --
`rtk grep -rt FOO .` consumes FOO as -t's value regardless of clap.

Adds clap-layer tests pinning the routing, including characterization tests
for behavior that was previously untested.
devnulled added a commit to devnulled/rtk that referenced this pull request Aug 30, 2026
1. The --file-type removal had ZERO coverage. Restoring the option long-only
   (no `-t`) left the entire suite green, so nothing stopped it creeping
   back. Added test_grep_file_type_option_is_gone, which pins that the flag
   routes to extra_args. Note it is NOT a clap error -- unknown long flags
   fall through to the trailing slot -- so an is_err() assertion would be
   wrong; grep is what rejects it. Mutation-proved:

       # --file-type restored long-only (+ file_type: _ so it compiles)
       assertion `left == right` failed
         left: ["FOO", "src/"]
        right: ["--file-type", "rust", "FOO", "src/"]
       test result: FAILED. 0 passed; 1 failed

2. max_len_long_option_still_binds_to_rtk guarded on rg_available(), but
   `rtk grep` execs grep, never rg (search.rs: `Engine::Grep => "grep"`).
   On a box without ripgrep the test skipped and reported success, hiding
   the mutant it exists to catch. Added grep_available() and switched to it.
   Proved with the call site discarding max_len and rg off PATH:

       $ PATH=/usr/bin:/bin search_compress_test max_len_long_option
       --max-len must still bind after the short form was removed
       (narrow=83, default=83)
       test result: FAILED. 0 passed; 1 failed

   Before this change that invocation printed `ok`.

3. test_grep_options_after_pattern_stay_in_extra_args claimed to
   demonstrate a `trailing_var_arg` gotcha. Removing trailing_var_arg leaves
   it green -- `allow_hyphen_values` is what produces the behavior. Comment
   corrected so it is not read as a guard on trailing_var_arg.

Also drops test_grep_parse_max_count_m_forwarded: it duplicates the
pre-existing test_try_parse_grep_dash_m_is_max_count (src/main.rs:3012),
which dies to the same mutant and asserts strictly more (it also pins
`max == 200`).

Walks back the Grep NOTE block, which overclaimed. It said this "completes"
the shadowed-short cleanup. It does not:
  - clap's auto `-h` still beats grep/rg's --no-filename before the first
    positional, and after it reaches search.rs's help check and execs
    unfiltered. That is rtk-ai#2532's scope, so the comment now says so rather
    than inviting the next reader to add disable_help_flag here.
  - removing a clap short does not make the letter safe end-to-end.
    search.rs shares one VALUE_FLAGS_SHORT table across both engines, so a
    letter taking a value in rg but not grep still eats the next token in a
    cluster: `rtk grep -rt FOO .` consumes FOO as -t's value no matter what
    clap does. Noted as a search.rs defect, not fixed here.

Separately, .claude/hooks/rtk-suggest.sh mapped both `rg` and `grep` to
`rtk grep`, swapping engines. With --file-type gone, its suggestion for
`rg -t rust FOO` became `rtk grep -t rust FOO`, which grep rejects. Now
maps rg -> `rtk rg` and grep -> `rtk grep`.
@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Closing in favour of #3089 for the Claude worktree exclusion and #3085 for the grep -h passthrough. Both land on the current src/cmds/system/search.rs, which replaced grep_cmd.rs, and both come with end to end tests. Please take any case they miss to those PRs.

@KuSh KuSh closed this Sep 6, 2026
@KuSh KuSh added the duplicate This issue or pull request already exists label Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk grep -h prints help instead of searching — clap intercepts GNU grep's -h (--no-filename), exit 0, no native fallback

2 participants