From 11a8f0eaf0f661c5b20c20fa2399314905d84fc1 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 10 Mar 2018 10:34:35 -0500 Subject: [PATCH] args: treat --count --only-matching as --count-matches Namely, when ripgrep is asked to count things and is also asked to print every match on its own line, then we should just automatically count the matches and not the lines. This is a departure from how GNU grep behaves, but there is a compelling argument to be made that GNU grep's behavior doesn't make a lot of sense. Note that since this changes the behavior of combining two existing flags, this is a breaking change. --- src/app.rs | 6 ++++-- src/args.rs | 10 ++++++++-- tests/tests.rs | 7 +++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9d1d734c..bfa2c708 100644 --- a/src/app.rs +++ b/src/app.rs @@ -784,7 +784,8 @@ If only one file is given to ripgrep, then only the count is printed if there is a match. The --with-filename flag can be used to force printing the file path in this case. -This overrides the --count-matches flag. +This overrides the --count-matches flag. Note that when --count is combined +with --only-matching, then ripgrep behaves as if --count-matches was given. "); let arg = RGArg::switch("count").short("c") .help(SHORT).long_help(LONG).overrides("count-matches"); @@ -805,7 +806,8 @@ If only one file is given to ripgrep, then only the count is printed if there is a match. The --with-filename flag can be used to force printing the file path in this case. -This overrides the --count flag. +This overrides the --count flag. Note that when --count is combined with +--only-matching, then ripgrep behaves as if --count-matches was given. "); let arg = RGArg::switch("count-matches") .help(SHORT).long_help(LONG).overrides("count"); diff --git a/src/args.rs b/src/args.rs index 309f5db8..a5e13415 100644 --- a/src/args.rs +++ b/src/args.rs @@ -747,10 +747,16 @@ impl<'a> ArgMatches<'a> { let count = self.is_present("count"); let count_matches = self.is_present("count-matches"); let invert_matches = self.is_present("invert-match"); + let only_matching = self.is_present("only-matching"); if count_matches && invert_matches { - return (true, false); + // Treat `-v --count-matches` as `-v -c`. + (true, false) + } else if count && only_matching { + // Treat `-c --only-matching` as `--count-matches`. + (false, true) + } else { + (count, count_matches) } - (count, count_matches) } /// Returns the user's color choice based on command line parameters and diff --git a/tests/tests.rs b/tests/tests.rs index b35f23f6..4ffb6d6d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -426,6 +426,13 @@ sherlock!(count_matches_inverted, "Sherlock", ".", |wd: WorkDir, mut cmd: Comman assert_eq!(lines, expected); }); +sherlock!(count_matches_via_only, "the", ".", |wd: WorkDir, mut cmd: Command| { + cmd.arg("--count").arg("--only-matching"); + let lines: String = wd.stdout(&mut cmd); + let expected = "sherlock:4\n"; + assert_eq!(lines, expected); +}); + sherlock!(files_with_matches, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| { cmd.arg("--files-with-matches"); let lines: String = wd.stdout(&mut cmd);