Commit | Line | Data |
3fea05b9 |
1 | #!/usr/bin/perl -w |
2 | |
3 | eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' |
4 | if 0; # not running under some shell |
5 | use strict; |
6 | use File::Find::Rule; |
7 | use File::Spec::Functions qw(catdir); |
8 | |
9 | # bootstrap extensions |
10 | for (@INC) { |
11 | my $dir = catdir($_, qw( File Find Rule ) ); |
12 | next unless -d $dir; |
13 | my @pm = find( name => '*.pm', maxdepth => 1, |
14 | exec => sub { (my $name = $_) =~ s/\.pm$//; |
15 | eval "require File::Find::Rule::$name"; }, |
16 | in => $dir ); |
17 | } |
18 | |
19 | # what directories are we searching in? |
20 | my @where; |
21 | while (@ARGV) { |
22 | local $_ = shift @ARGV; |
23 | if (/^-/) { |
24 | unshift @ARGV, $_; |
25 | last; |
26 | } |
27 | push @where, $_; |
28 | } |
29 | |
30 | # parse arguments, build a rule object |
31 | my $rule = new File::Find::Rule; |
32 | while (@ARGV) { |
33 | my $clause = shift @ARGV; |
34 | |
35 | unless ( $clause =~ s/^-// && $rule->can( $clause ) ) { |
36 | # not a known rule - complain about this |
37 | die "unknown option '$clause'\n" |
38 | } |
39 | |
40 | # it was the last switch |
41 | unless (@ARGV) { |
42 | $rule->$clause(); |
43 | next; |
44 | } |
45 | |
46 | # consume the parameters |
47 | my $param = shift @ARGV; |
48 | |
49 | if ($param =~ /^-/) { |
50 | # it's the next switch - put it back, and add one with no params |
51 | unshift @ARGV, $param; |
52 | $rule->$clause(); |
53 | next; |
54 | } |
55 | |
56 | if ($param eq '(') { |
57 | # multiple values - just look for the closing parenthesis |
58 | my @p; |
59 | while (@ARGV) { |
60 | my $val = shift @ARGV; |
61 | last if $val eq ')'; |
62 | push @p, $val; |
63 | } |
64 | $rule->$clause( @p ); |
65 | next; |
66 | } |
67 | |
68 | # a single argument |
69 | $rule->$clause( $param ); |
70 | } |
71 | |
72 | # add a print rule so things happen faster |
73 | $rule->exec( sub { print "$_[2]\n"; return; } ); |
74 | |
75 | # profit |
76 | $rule->in( @where ? @where : '.' ); |
77 | exit 0; |
78 | |
79 | __END__ |
80 | |
81 | =head1 NAME |
82 | |
83 | findrule - command line wrapper to File::Find::Rule |
84 | |
85 | =head1 USAGE |
86 | |
87 | findrule [path...] [expression] |
88 | |
89 | =head1 DESCRIPTION |
90 | |
91 | C<findrule> mostly borrows the interface from GNU find(1) to provide a |
92 | command-line interface onto the File::Find::Rule heirarchy of modules. |
93 | |
94 | The syntax for expressions is the rule name, preceded by a dash, |
95 | followed by an optional argument. If the argument is an opening |
96 | parenthesis it is taken as a list of arguments, terminated by a |
97 | closing parenthesis. |
98 | |
99 | Some examples: |
100 | |
101 | find -file -name ( foo bar ) |
102 | |
103 | files named C<foo> or C<bar>, below the current directory. |
104 | |
105 | find -file -name foo -bar |
106 | |
107 | files named C<foo>, that have pubs (for this is what our ficticious |
108 | C<bar> clause specifies), below the current directory. |
109 | |
110 | find -file -name ( -bar ) |
111 | |
112 | files named C<-bar>, below the current directory. In this case if |
113 | we'd have omitted the parenthesis it would have parsed as a call to |
114 | name with no arguments, followed by a call to -bar. |
115 | |
116 | =head2 Supported switches |
117 | |
118 | I'm very slack. Please consult the File::Find::Rule manpage for now, |
119 | and prepend - to the commands that you want. |
120 | |
121 | =head2 Extra bonus switches |
122 | |
123 | findrule automatically loads all of your installed File::Find::Rule::* |
124 | extension modules, so check the documentation to see what those would be. |
125 | |
126 | =head1 AUTHOR |
127 | |
128 | Richard Clamp <richardc@unixbeard.net> from a suggestion by Tatsuhiko Miyagawa |
129 | |
130 | =head1 COPYRIGHT |
131 | |
132 | Copyright (C) 2002 Richard Clamp. All Rights Reserved. |
133 | |
134 | This program is free software; you can redistribute it and/or modify it |
135 | under the same terms as Perl itself. |
136 | |
137 | =head1 SEE ALSO |
138 | |
139 | L<File::Find::Rule> |
140 | |
141 | =cut |