App-File-Grepper
view release on metacpan or search on metacpan
# Process command line options, config files, and such.
my $options = app_setup( "afg", $App::File::Grepper::VERSION );
################ Presets ################
$options->{trace} = 1 if $options->{debug};
$options->{verbose} = 1 if $options->{trace};
################ Activate ################
App::File::Grepper->main( $options, @ARGV );
################ Options and Configuration ################
use Getopt::Long 2.13;
# Package name.
my $my_package;
# Program name and version.
my ($my_name, $my_version);
sub app_setup {
my ($appname, $appversion, %args) = @_;
my $help = 0; # handled locally
my $ident = 0; # handled locally
# Package name.
$my_package = $args{package};
# Program name and version.
if ( defined $appname ) {
($my_name, $my_version) = ($appname, $appversion);
}
else {
($my_name, $my_version) = qw( MyProg 0.01 );
}
my $options =
{
verbose => 0, # verbose processing
### ADD OPTIONS HERE ###
'ignorecase' => undef,
'filter' => undef,
'exclude' => undef,
'edit-with-emacs'=> 0,
'edit-with-vi' => 0,
'view' => 0,
'pattern' => undef,
# Development options (not shown with -help).
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 ",
( run in 1.426 second using v1.01-cache-2.11-cpan-39bf76dae61 )