Blame findrule

Packit 6f982a
#!perl -w
Packit 6f982a
use strict;
Packit 6f982a
use File::Find::Rule;
Packit 6f982a
use File::Spec::Functions qw(catdir);
Packit 6f982a
Packit 6f982a
# bootstrap extensions
Packit 6f982a
for (@INC) {
Packit 6f982a
    my $dir = catdir($_, qw( File Find Rule ) );
Packit 6f982a
    next unless -d $dir;
Packit 6f982a
    my @pm = find( name => '*.pm', maxdepth => 1,
Packit 6f982a
                   exec => sub { (my $name = $_) =~ s/\.pm$//;
Packit 6f982a
                                 eval "require File::Find::Rule::$name"; },
Packit 6f982a
                   in => $dir );
Packit 6f982a
}
Packit 6f982a
Packit 6f982a
# what directories are we searching in?
Packit 6f982a
my @where;
Packit 6f982a
while (@ARGV) {
Packit 6f982a
    local $_ = shift @ARGV;
Packit 6f982a
    if (/^-/) {
Packit 6f982a
        unshift @ARGV, $_;
Packit 6f982a
        last;
Packit 6f982a
    }
Packit 6f982a
    push @where, $_;
Packit 6f982a
}
Packit 6f982a
Packit 6f982a
# parse arguments, build a rule object
Packit 6f982a
my $rule = new File::Find::Rule;
Packit 6f982a
while (@ARGV) {
Packit 6f982a
    my $clause = shift @ARGV;
Packit 6f982a
Packit 6f982a
    unless ( $clause =~ s/^-// && $rule->can( $clause ) ) {
Packit 6f982a
        # not a known rule - complain about this
Packit 6f982a
        die "unknown option '$clause'\n"
Packit 6f982a
    }
Packit 6f982a
Packit 6f982a
    # it was the last switch
Packit 6f982a
    unless (@ARGV) {
Packit 6f982a
        $rule->$clause();
Packit 6f982a
        next;
Packit 6f982a
    }
Packit 6f982a
Packit 6f982a
    # consume the parameters
Packit 6f982a
    my $param = shift @ARGV;
Packit 6f982a
Packit 6f982a
    if ($param =~ /^-/) {
Packit 6f982a
        # it's the next switch - put it back, and add one with no params
Packit 6f982a
        unshift @ARGV, $param;
Packit 6f982a
        $rule->$clause();
Packit 6f982a
        next;
Packit 6f982a
    }
Packit 6f982a
Packit 6f982a
    if ($param eq '(') {
Packit 6f982a
        # multiple values - just look for the closing parenthesis
Packit 6f982a
        my @p;
Packit 6f982a
        while (@ARGV) {
Packit 6f982a
            my $val = shift @ARGV;
Packit 6f982a
            last if $val eq ')';
Packit 6f982a
            push @p, $val;
Packit 6f982a
        }
Packit 6f982a
        $rule->$clause( @p );
Packit 6f982a
        next;
Packit 6f982a
    }
Packit 6f982a
Packit 6f982a
    # a single argument
Packit 6f982a
    $rule->$clause( $param );
Packit 6f982a
}
Packit 6f982a
Packit 6f982a
# add a print rule so things happen faster
Packit 6f982a
$rule->exec( sub { print "$_[2]\n"; return; } );
Packit 6f982a
Packit 6f982a
# profit
Packit 6f982a
$rule->in( @where ? @where : '.' );
Packit 6f982a
exit 0;
Packit 6f982a
Packit 6f982a
__END__
Packit 6f982a
Packit 6f982a
=head1 NAME
Packit 6f982a
Packit 6f982a
findrule - command line wrapper to File::Find::Rule
Packit 6f982a
Packit 6f982a
=head1 USAGE
Packit 6f982a
Packit 6f982a
  findrule [path...] [expression]
Packit 6f982a
Packit 6f982a
=head1 DESCRIPTION
Packit 6f982a
Packit 6f982a
C<findrule> mostly borrows the interface from GNU find(1) to provide a
Packit 6f982a
command-line interface onto the File::Find::Rule heirarchy of modules.
Packit 6f982a
Packit 6f982a
The syntax for expressions is the rule name, preceded by a dash,
Packit 6f982a
followed by an optional argument.  If the argument is an opening
Packit 6f982a
parenthesis it is taken as a list of arguments, terminated by a
Packit 6f982a
closing parenthesis.
Packit 6f982a
Packit 6f982a
Some examples:
Packit 6f982a
Packit 6f982a
 find -file -name ( foo bar )
Packit 6f982a
Packit 6f982a
files named C<foo> or C<bar>, below the current directory.
Packit 6f982a
Packit 6f982a
 find -file -name foo -bar
Packit 6f982a
Packit 6f982a
files named C<foo>, that have pubs (for this is what our ficticious
Packit 6f982a
C<bar> clause specifies), below the current directory.
Packit 6f982a
Packit 6f982a
 find -file -name ( -bar )
Packit 6f982a
Packit 6f982a
files named C<-bar>, below the current directory.  In this case if
Packit 6f982a
we'd have omitted the parenthesis it would have parsed as a call to
Packit 6f982a
name with no arguments, followed by a call to -bar.
Packit 6f982a
Packit 6f982a
=head2 Supported switches
Packit 6f982a
Packit 6f982a
I'm very slack.  Please consult the File::Find::Rule manpage for now,
Packit 6f982a
and prepend - to the commands that you want.
Packit 6f982a
Packit 6f982a
=head2 Extra bonus switches
Packit 6f982a
Packit 6f982a
findrule automatically loads all of your installed File::Find::Rule::*
Packit 6f982a
extension modules, so check the documentation to see what those would be.
Packit 6f982a
Packit 6f982a
=head1 AUTHOR
Packit 6f982a
Packit 6f982a
Richard Clamp <richardc@unixbeard.net> from a suggestion by Tatsuhiko Miyagawa
Packit 6f982a
Packit 6f982a
=head1 COPYRIGHT
Packit 6f982a
Packit 6f982a
Copyright (C) 2002 Richard Clamp.  All Rights Reserved.
Packit 6f982a
Packit 6f982a
This program is free software; you can redistribute it and/or modify it
Packit 6f982a
under the same terms as Perl itself.
Packit 6f982a
Packit 6f982a
=head1 SEE ALSO
Packit 6f982a
Packit 6f982a
L<File::Find::Rule>
Packit 6f982a
Packit 6f982a
=cut