Add built local::lib
[catagits/Gitalist.git] / local-lib5 / bin / findrule
CommitLineData
3fea05b9 1#!/usr/bin/perl -w
2
3eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5use strict;
6use File::Find::Rule;
7use File::Spec::Functions qw(catdir);
8
9# bootstrap extensions
10for (@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?
20my @where;
21while (@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
31my $rule = new File::Find::Rule;
32while (@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 : '.' );
77exit 0;
78
79__END__
80
81=head1 NAME
82
83findrule - command line wrapper to File::Find::Rule
84
85=head1 USAGE
86
87 findrule [path...] [expression]
88
89=head1 DESCRIPTION
90
91C<findrule> mostly borrows the interface from GNU find(1) to provide a
92command-line interface onto the File::Find::Rule heirarchy of modules.
93
94The syntax for expressions is the rule name, preceded by a dash,
95followed by an optional argument. If the argument is an opening
96parenthesis it is taken as a list of arguments, terminated by a
97closing parenthesis.
98
99Some examples:
100
101 find -file -name ( foo bar )
102
103files named C<foo> or C<bar>, below the current directory.
104
105 find -file -name foo -bar
106
107files named C<foo>, that have pubs (for this is what our ficticious
108C<bar> clause specifies), below the current directory.
109
110 find -file -name ( -bar )
111
112files named C<-bar>, below the current directory. In this case if
113we'd have omitted the parenthesis it would have parsed as a call to
114name with no arguments, followed by a call to -bar.
115
116=head2 Supported switches
117
118I'm very slack. Please consult the File::Find::Rule manpage for now,
119and prepend - to the commands that you want.
120
121=head2 Extra bonus switches
122
123findrule automatically loads all of your installed File::Find::Rule::*
124extension modules, so check the documentation to see what those would be.
125
126=head1 AUTHOR
127
128Richard Clamp <richardc@unixbeard.net> from a suggestion by Tatsuhiko Miyagawa
129
130=head1 COPYRIGHT
131
132Copyright (C) 2002 Richard Clamp. All Rights Reserved.
133
134This program is free software; you can redistribute it and/or modify it
135under the same terms as Perl itself.
136
137=head1 SEE ALSO
138
139L<File::Find::Rule>
140
141=cut