Skip to content

Commit

Permalink
Add support for searching multiple patterns with -e.
Browse files Browse the repository at this point in the history
Also, change -Q/--literal to -F/--fixed-strings because compatibility
with grep is probably better.
  • Loading branch information
BurntSushi committed Sep 17, 2016
1 parent 0a63158 commit f7ee914
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
58 changes: 39 additions & 19 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@ 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] <pattern> [<path> ...]
Usage: rg [options] -e PATTERN ... [<path> ...]
rg [options] <pattern> [<path> ...]
rg [options] --files [<path> ...]
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.
-c, --count Only show count of line matches for each file.
--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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand All @@ -187,6 +190,7 @@ pub struct RawArgs {
flag_no_mmap: bool,
flag_pretty: bool,
flag_quiet: bool,
flag_regexp: Vec<String>,
flag_replace: Option<String>,
flag_text: bool,
flag_threads: usize,
Expand Down Expand Up @@ -236,19 +240,7 @@ pub struct Args {
impl RawArgs {
/// Convert arguments parsed into a configuration used by ripgrep.
fn to_args(&self) -> Result<Args> {
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()
Expand Down Expand Up @@ -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::<Vec<String>>().join("|")
} else {
self.flag_regexp.iter().cloned().map(|pat| {
self.word_pattern(pat)
}).collect::<Vec<String>>().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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit f7ee914

Please sign in to comment.