Skip to content

Accept -e/-f patterns without a positional pattern in search - #232

Merged
Shengyu Fu (shengyfu) merged 1 commit into
microsoft:mainfrom
Amorth:fix-search-pattern-with-regexp
Nov 30, 2026
Merged

Shengyu Fu (shengyfu) merged 1 commit into
microsoft:mainfrom
Amorth:fix-search-pattern-with-regexp

Conversation

@Amorth

Copy link
Copy Markdown
Contributor

The search subcommand rejects -e/-f patterns when no path follows:

$ printf 'foo\nbar\n' > a.txt; printf 'foo\n' > pats
$ tgrep --no-index search -e foo
error: the following required arguments were not provided:
  <PATTERN>

Usage: tgrep search --regexp <REGEXP> <PATTERN> [PATH]...
$ tgrep --no-index search -f pats      # same error
$ tgrep --no-index -e foo search       # same error
$ tgrep --no-index -e foo              # works
pats:foo
a.txt:foo

rg -e foo and rg -f pats (ripgrep 14.1.1) search the current directory, and so does tgrep's top-level form. With a path (tgrep search -e foo .) the subcommand already works, because the path is first parsed as the pattern and then moved back to the paths by split_pattern_and_paths.

Cause: in Command::Search the positional pattern is a required String, so clap demands it before -e/-f are taken into account. The top-level pattern is already an Option<String>.

Change: make the search pattern an Option<String> and pass it to split_pattern_and_paths as the top-level form does. The pattern is still required when no -e/-f pattern is given; that check runs in try_parse_args_from after the command scopes are merged, because -e/-f are global and a parent command's patterns (tgrep -e foo search) must count as well. Clap validates each scope on its own, so required_unless_present_any on the subcommand would not see them. tgrep search with no pattern still fails with the same "required arguments" error and exit code 2.

Tests:

  • search_subcommand_accepts_patterns_from_regexp_or_file_without_positional in tgrep-cli/tests/ripgrep_compat.rs runs search -e foo, search -f <file>, -e foo search and -f <file> search in a directory and checks the match, and checks that a bare search still exits 2 naming <PATTERN>. It fails without the change (exit 2) and passes with it.
  • search_subcommand_pattern_is_optional_with_regexp_or_file in main.rs checks the parsed pattern/paths split for these forms, with and without paths, and the missing-pattern error kind.

cargo fmt --all -- --check is clean. cargo clippy --locked --workspace --all-targets --features tgrep-cli/managed-test-hooks -- -D warnings reports only two nonminimal_bool errors in serve.rs and serve/shared/managed.rs, which this change does not touch (same on main with Rust 1.93.1 and 1.95); it is otherwise clean. cargo test --locked --workspace --features tgrep-cli/managed-test-hooks -- --test-threads=2: ripgrep_compat 270 passed, ripgrep_parity 102 passed. The managed/shared-index tests fail on my machine because its Git 2.34 lacks git worktree list -z; the same 250 tests fail on main there.

`tgrep search -e foo` and `tgrep search -f patterns` failed with
"the following required arguments were not provided: <PATTERN>"
when no path was given, although the top-level form and ripgrep accept
them. The subcommand's positional pattern was a required String, so
clap demanded it before -e/-f were considered.

Make it optional and require it only when no -e/-f pattern was given in
any command scope, after the scopes are merged, so a parent command's
patterns (`tgrep -e foo search`) count too.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 15:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the changes allowing search to use -e/-f patterns without a positional pattern.

No open issues 🎉

What changed in this PR

This PR aligns the search subcommand with tgrep’s top-level behavior, allowing -e/-f patterns without a positional pattern while still rejecting searches with no pattern source.

Changes:

  • Makes the positional pattern optional and validates it after global argument scopes are merged.
  • Passes the optional pattern through the existing pattern/path splitting logic.
  • Adds unit and integration tests for flags before and after search, path handling, and missing-pattern errors.
File Description
tgrep-cli/​tests/​ripgrep_compat.rs Adds CLI regression tests for pattern-source flags and bare search errors.
tgrep-cli/​src/​main.rs Updates pattern parsing and validation, with unit tests for argument handling.

Review effort: Balanced

@Amorth

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@shengyfu
Shengyu Fu (shengyfu) merged commit 9947eef into microsoft:main Nov 30, 2026
10 checks passed
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.

3 participants