Skip to content

Commit 82485bf

Browse files
committed
feat: Add --exact argument to match exact filename
1 parent 0e74d5b commit 82485bf

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/cli.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,30 @@ pub struct Opts {
173173

174174
/// Treat the pattern as a literal string instead of a regular expression. Note
175175
/// that this also performs substring comparison. If you want to match on an
176-
/// exact filename, consider using '--glob'.
176+
/// exact filename, consider using '--glob' or '--exact' instead.
177177
#[arg(
178178
long,
179179
short = 'F',
180180
alias = "literal",
181181
hide_short_help = true,
182-
help = "Treat pattern as literal string stead of regex",
182+
help = "Treat pattern as literal string instead of regex",
183183
long_help
184184
)]
185185
pub fixed_strings: bool,
186186

187+
/// Perform an exact match. This is equivalent to '--fixed-strings' but requires
188+
/// the pattern to match the entire filename (or path if '--full-path' is used),
189+
/// rather than a substring. Special regex characters in the pattern are treated
190+
/// as literal characters.
191+
#[arg(
192+
long,
193+
conflicts_with("glob"),
194+
hide_short_help = true,
195+
help = "Match the entire filename exactly (literal, non-substring)",
196+
long_help
197+
)]
198+
pub exact: bool,
199+
187200
/// Add additional required search patterns, all of which must be matched. Multiple
188201
/// additional patterns can be specified. The patterns are regular
189202
/// expressions, unless '--glob' or '--fixed-strings' is used.

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ fn build_pattern_regex(pattern: &str, opts: &Opts) -> Result<String> {
170170
Ok(if opts.glob && !pattern.is_empty() {
171171
let glob = GlobBuilder::new(pattern).literal_separator(true).build()?;
172172
glob.regex().to_owned()
173+
} else if opts.exact {
174+
// Anchor the escaped pattern so the full filename (or path) must match exactly.
175+
// Literal. No substring matching.
176+
format!("^{}$", regex::escape(pattern))
173177
} else if opts.fixed_strings {
174178
// Treat pattern as literal string if '--fixed-strings' is used
175179
regex::escape(pattern)

0 commit comments

Comments
 (0)