App-File-Grepper

 view release on metacpan or  search on metacpan

script/afg  view on Meta::CPAN

       debug		=> 0,		# debugging
       trace		=> 0,		# trace (show process)

       # Service.
       _package		=> $my_package,
       _name		=> $my_name,
       _version		=> $my_version,
       _stdin		=> \*STDIN,
       _stdout		=> \*STDOUT,
       _stderr		=> \*STDERR,
       _argv		=> [ @ARGV ],
      };

    # Colled command line options in a hash, for they will be needed
    # later.
    my $clo = {};

    Getopt::Long::Configure('noignorecase');
    # Sorry, layout is a bit ugly...
    if ( !GetOptions
	 ($clo,

	  ### ADD OPTIONS HERE ###

	 'ignorecase|i',
	 'filter|G=s',
	 'exclude|E=s',
	 'edit-with-emacs|e',
	 'edit-with-vi|v',
	 'pattern=s',
	 'view',

	  # Standard options.
	  'ident'		=> \$ident,
	  'help|?'		=> \$help,
	  'verbose',
	  'trace',
	  'debug',
	 ) )
    {
	# GNU convention: message to STDERR upon failure.
	app_usage(\*STDERR, 2);
    }
    # GNU convention: message to STDOUT upon request.
    app_usage(\*STDOUT, 0) if $help;
    app_ident(\*STDOUT) if $ident;

    unless ( defined $clo->{pattern} ) {
	$clo->{pattern} = shift( @ARGV );
    }
    app_usage(\*STDERR, 1) unless @ARGV;
    if ( $ENV{LANG} =~ /\.utf-?8$/i ) {
	require Encode;
	$clo->{pattern} = Encode::decode( "utf8", $clo->{pattern} );
    }

    # Plug in command-line options.
    @{$options}{keys %$clo} = values %$clo;

    if ( $options->{debug} ) {
	use Data::Dumper;
	warn Dumper $options;
    }

    $options;
}

sub app_ident {
    my ($fh) = @_;
    print {$fh} ("This is ",
		 $my_package
		 ? "$my_package [$my_name $my_version]"
		 : "$my_name version $my_version",
		 "\n");
}

sub app_usage {
    my ($fh, $exit) = @_;
    app_ident($fh);
    print ${fh} <<EndOfUsage;
Usage: $0 [options] pattern directories...

Application options:
    --ignorecase  -i	ignore case while matching
    --filter=PAT  -G	filter filenames according to PAT
    --exclude=PAT -E	reject filenames according to PAT
    --edit-with-emacs  -e   pass each file that matches to emacs
    --edit-with-vi  -v	pass each file that matches to vi
    --pattern=PAT	explicitly specify the pattern
    --view		view each file that matches

Miscellaneous options:
    --help		this message
    --ident		show identification
    --verbose		verbose information
EndOfUsage
    exit $exit if defined $exit;
}

=head1 NAME

afg - Greps files for pattern

=head1 SYNOPSIS

Runs a File::Find on the specified directories, and greps all text
files for a pattern.

This script is a driver for L<App::File::Grepper>, which see.

=head1 COMMAND LINE OPTIONS

=over 4

=item B<--pattern=>I<pattern>

App::File::Grepper option: B<pattern>.

If no B<--pattern> option is supplied, it is taken from the command line.

=item B<--ignorecase> or B<-i>



( run in 0.835 second using v1.01-cache-2.11-cpan-39bf76dae61 )