Skip to content

Commit

Permalink
Added option --match to explicitly specify a regex on the command lin…
Browse files Browse the repository at this point in the history
…e (see issue 64).
  • Loading branch information
torsten.blix committed Feb 18, 2008
1 parent 216f4a1 commit c272b43
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Ack.pm
Expand Up @@ -183,6 +183,7 @@ sub get_command_line_options {
'l|files-with-matches' => \$opt{l},
'L|files-without-match' => sub { $opt{l} = $opt{v} = 1 },
'm|max-count=i' => \$opt{m},
'match=s' => \$opt{regex},
n => \$opt{n},
o => sub { $opt{output} = '$&' },
'output=s' => \$opt{output},
Expand Down Expand Up @@ -588,6 +589,7 @@ Searching:
-v, --invert-match Invert match: select non-matching lines
-w, --word-regexp Force PATTERN to match only whole words
-Q, --literal Quote all metacharacters; expr is literal
--match REGEX Specify REGEX explicitly.
Search output:
--line=NUM Only print line(s) NUM of each file
Expand Down
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -7,6 +7,8 @@ Changelog for ack
Added a --[no]ignore-dir option, to include/exclude additional
directories. Many thanks for this patch go to Matthew Wickline!

Added option --match REGEX to specify a regex explicitly.

[FIXES]
Fixed bug with -v not finding all files correctly (introduced
with the performance optimizations in ack 1.75)
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -22,6 +22,7 @@ t/ack-group.t
t/ack-h.t
t/ack-ignore-dir.t
t/ack-line.t
t/ack-match.t
t/ack-o.t
t/ack-passthru.t
t/ack-print0.t
Expand Down
14 changes: 12 additions & 2 deletions ack
Expand Up @@ -53,7 +53,7 @@ sub main {
$opt{$_} and App::Ack::die( "Can't use -$_ when acting as a filter." );
}
$opt{show_filename} = 0;
$opt{regex} = App::Ack::build_regex( shift @ARGV, \%opt );
$opt{regex} = App::Ack::build_regex( defined $opt{regex} ? $opt{regex} : shift @ARGV, \%opt );
if ( my $nargs = @ARGV ) {
my $s = $nargs == 1 ? '' : 's';
App::Ack::warn( "Ignoring $nargs argument$s on the command-line while acting as a filter." );
Expand All @@ -65,7 +65,7 @@ sub main {
my $file_matching = $opt{f} || $opt{g} || $opt{lines};
if ( !$file_matching ) {
@ARGV or App::Ack::die( 'No regular expression found.' );
$opt{regex} = App::Ack::build_regex( shift @ARGV, \%opt );
$opt{regex} = App::Ack::build_regex( defined $opt{regex} ? $opt{regex} : shift @ARGV, \%opt );
}

my @what;
Expand Down Expand Up @@ -319,6 +319,16 @@ order given on the command line.
Only print the filenames of matching files, instead of the matching text.
=item B<--match I<REGEX>>
Specify the I<REGEX> explicitly. This is helpful if you don't want to put the
regex as your first argument, e.g. when executing multiple searches over the
same set of files.
# search for foo and bar in given files
ack file1 t/file* --match foo
ack file1 t/file* --match bar
=item B<-m=I<NUM>>, B<--max-count=I<NUM>>
Stop reading a file after I<NUM> matches.
Expand Down
1 change: 1 addition & 0 deletions ack-help.txt
Expand Up @@ -16,6 +16,7 @@ Searching:
-v, --invert-match Invert match: select non-matching lines
-w, --word-regexp Force PATTERN to match only whole words
-Q, --literal Quote all metacharacters; expr is literal
--match REGEX Specify REGEX explicitly.

Search output:
--line=NUM Only print line(s) NUM of each file
Expand Down
16 changes: 14 additions & 2 deletions ack-standalone
Expand Up @@ -51,7 +51,7 @@ sub main {
$opt{$_} and App::Ack::die( "Can't use -$_ when acting as a filter." );
}
$opt{show_filename} = 0;
$opt{regex} = App::Ack::build_regex( shift @ARGV, \%opt );
$opt{regex} = App::Ack::build_regex( defined $opt{regex} ? $opt{regex} : shift @ARGV, \%opt );
if ( my $nargs = @ARGV ) {
my $s = $nargs == 1 ? '' : 's';
App::Ack::warn( "Ignoring $nargs argument$s on the command-line while acting as a filter." );
Expand All @@ -63,7 +63,7 @@ sub main {
my $file_matching = $opt{f} || $opt{g} || $opt{lines};
if ( !$file_matching ) {
@ARGV or App::Ack::die( 'No regular expression found.' );
$opt{regex} = App::Ack::build_regex( shift @ARGV, \%opt );
$opt{regex} = App::Ack::build_regex( defined $opt{regex} ? $opt{regex} : shift @ARGV, \%opt );
}

my @what;
Expand Down Expand Up @@ -317,6 +317,16 @@ order given on the command line.
Only print the filenames of matching files, instead of the matching text.
=item B<--match I<REGEX>>
Specify the I<REGEX> explicitly. This is helpful if you don't want to put the
regex as your first argument, e.g. when executing multiple searches over the
same set of files.
# search for foo and bar in given files
ack file1 t/file* --match foo
ack file1 t/file* --match bar
=item B<-m=I<NUM>>, B<--max-count=I<NUM>>
Stop reading a file after I<NUM> matches.
Expand Down Expand Up @@ -990,6 +1000,7 @@ sub get_command_line_options {
'l|files-with-matches' => \$opt{l},
'L|files-without-match' => sub { $opt{l} = $opt{v} = 1 },
'm|max-count=i' => \$opt{m},
'match=s' => \$opt{regex},
n => \$opt{n},
o => sub { $opt{output} = '$&' },
'output=s' => \$opt{output},
Expand Down Expand Up @@ -1325,6 +1336,7 @@ Searching:
-v, --invert-match Invert match: select non-matching lines
-w, --word-regexp Force PATTERN to match only whole words
-Q, --literal Quote all metacharacters; expr is literal
--match REGEX Specify REGEX explicitly.
Search output:
--line=NUM Only print line(s) NUM of each file
Expand Down
36 changes: 36 additions & 0 deletions t/ack-match.t
@@ -0,0 +1,36 @@
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use lib 't';
use Util;

my @files = qw( t/text );

my @tests = (
[ qw/Sue -a/ ],
[ qw/boy -a -i/ ], # case-insensitive is handled correctly with --match
[ qw/ll+ -a -Q/ ], # quotemeta is handled correctly with --match
[ qw/gon -a -w/ ], # words is handled correctly with --match
);

# 3 tests for each call to test_match()
plan tests => @tests * 3;

test_match( @$_ ) for @tests;


# call ack normally and compare output to calling with --match regex
#
# due to 2 calls to run_ack, this sub runs altogether 3 tests
sub test_match {
my $regex = shift;
my @args = @_;

my @results_normal = run_ack( @args, $regex, @files );
my @results_match = run_ack( @args, @files, '--match', $regex );

sets_match( \@results_normal, \@results_match, "Same output for regex '$regex'." );
}
1 change: 1 addition & 0 deletions t/ack-text.t
Expand Up @@ -27,6 +27,7 @@ ACK_F_TEXT: {
t/ack-h.t
t/ack-ignore-dir.t
t/ack-line.t
t/ack-match.t
t/ack-o.t
t/ack-passthru.t
t/ack-print0.t
Expand Down
1 change: 1 addition & 0 deletions t/ack-type.t
Expand Up @@ -58,6 +58,7 @@ my $perl = [qw(
t/ack-h.t
t/ack-ignore-dir.t
t/ack-line.t
t/ack-match.t
t/ack-o.t
t/ack-print0.t
t/ack-passthru.t
Expand Down

0 comments on commit c272b43

Please sign in to comment.