From f7ee914dd3f499d440747a0b4e7391ff2a5252cf Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 17 Sep 2016 16:55:58 -0400 Subject: Add support for searching multiple patterns with -e. Also, change -Q/--literal to -F/--fixed-strings because compatibility with grep is probably better. --- src/args.rs | 58 +++++++++++++++++++++++++++++++++++++++------------------- tests/tests.rs | 2 +- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/args.rs b/src/args.rs index 98c4afeb..3481333b 100644 --- a/src/args.rs +++ b/src/args.rs @@ -35,13 +35,14 @@ use Result; /// If you've never heard of Docopt before, see: http://docopt.org /// (TL;DR: The CLI parser is generated from the usage string below.) const USAGE: &'static str = " -Usage: rg [options] [ ...] +Usage: rg [options] -e PATTERN ... [ ...] + rg [options] [ ...] rg [options] --files [ ...] rg [options] --type-list rg --help rg --version -rg combines the usability of the silver search with the raw speed of grep. +rg combines the usability of The Silver Searcher with the raw speed of grep. Common options: -a, --text Search binary files as if they were text. @@ -49,6 +50,11 @@ Common options: --color WHEN Whether to use coloring in match. Valid values are never, always or auto. [default: auto] + -e, --regexp PATTERN ... Use PATTERN to search. This option can be + provided multiple times, where all patterns + given are searched. + -F, --fixed-strings Treat the pattern as a literal string instead of + a regular expression. -g, --glob GLOB ... Include or exclude files for searching that match the given glob. This always overrides any other ignore logic. Multiple glob flags may be @@ -134,9 +140,6 @@ Less common options: -p, --pretty Alias for --color=always --heading -n. - -Q, --literal - Treat the pattern as a literal string instead of a regular expression. - -j, --threads ARG The number of threads to use. Defaults to the number of logical CPUs (capped at 6). [default: 0] @@ -178,7 +181,7 @@ pub struct RawArgs { flag_ignore_case: bool, flag_invert_match: bool, flag_line_number: bool, - flag_literal: bool, + flag_fixed_strings: bool, flag_mmap: bool, flag_no_heading: bool, flag_no_ignore: bool, @@ -187,6 +190,7 @@ pub struct RawArgs { flag_no_mmap: bool, flag_pretty: bool, flag_quiet: bool, + flag_regexp: Vec, flag_replace: Option, flag_text: bool, flag_threads: usize, @@ -236,19 +240,7 @@ pub struct Args { impl RawArgs { /// Convert arguments parsed into a configuration used by ripgrep. fn to_args(&self) -> Result { - let pattern = { - let pattern = - if self.flag_literal { - regex::quote(&self.arg_pattern) - } else { - self.arg_pattern.clone() - }; - if self.flag_word_regexp { - format!(r"\b{}\b", pattern) - } else { - pattern - } - }; + let pattern = self.pattern(); let paths = if self.arg_path.is_empty() { if atty::on_stdin() @@ -380,6 +372,34 @@ impl RawArgs { } Ok(()) } + + fn pattern(&self) -> String { + if !self.flag_regexp.is_empty() { + if self.flag_fixed_strings { + self.flag_regexp.iter().cloned().map(|lit| { + self.word_pattern(regex::quote(&lit)) + }).collect::>().join("|") + } else { + self.flag_regexp.iter().cloned().map(|pat| { + self.word_pattern(pat) + }).collect::>().join("|") + } + } else { + if self.flag_fixed_strings { + self.word_pattern(regex::quote(&self.arg_pattern)) + } else { + self.word_pattern(self.arg_pattern.clone()) + } + } + } + + fn word_pattern(&self, s: String) -> String { + if self.flag_word_regexp { + format!(r"\b{}\b", s) + } else { + s + } + } } impl Args { diff --git a/tests/tests.rs b/tests/tests.rs index 903ef3e1..daff20da 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -166,7 +166,7 @@ For the Doctor Watsons of this world, as opposed to the Sherlock sherlock!(literal, "()", "file", |wd: WorkDir, mut cmd: Command| { wd.create("file", "blib\n()\nblab\n"); - cmd.arg("-Q"); + cmd.arg("-F"); let lines: String = wd.stdout(&mut cmd); assert_eq!(lines, "()\n"); }); -- cgit v1.2.3