use Getopt::Hash;
my %opt = GetOptions(
'option_name|on:s' => 'default_value',
'element|e!' => 0,
'count|cnt|c=i' => 5,
);
if ($opt{'option_name'} eq 'some_value') {
## do work ##
}
####
use Getopt::Long;
my %opt = (
'option_name' => 'default_value',
'element' => 0,
'count' => 5,
);
GetOptions(
'option_name|on:s' => \$opt{'option_name'},
'element|e!' => \$opt{'element'},
'count|cnt|c=i' => \$opt{'count'},
);
if ($opt{'option_name'} eq 'some_value') {
## do work ##
}
####
'getopt_spec_string' => 'default_value'
####
my %opt = GetOptions(
'option_name|on:s' => 'default_value',
'element|e!' => 0,
'count|cnt|c=i' => 5,
);
####
'option_name' => 'default_value',
'element' => 0,
'count' => 5,
####
package Getopt::Hash;
use strict; use warnings;
use vars '$VERSION';
$VERSION = '0.81';
#=============================================================================
# Hash.pm - module to use Getopt::Long to quickly populate an options hash
#-----------------------------------------------------------------------------
# Created : (0.80) 2005-08.Aug-05 by Darren Meyer
# Modified:
#-----------------------------------------------------------------------------
# Dependencies: Getopt::Long
#=============================================================================
require Exporter;
use vars '@ISA','@EXPORT','@CONFIG';
@ISA = 'Exporter';
@EXPORT = 'GetOptions';
require Getopt::Long;
### use %hash = GetOptions(%spec); %spec is like 'process=s' => 'default_val'
sub GetOptions {
my %spec = @_;
my %opt_hash;
my $gol = new Getopt::Long::Parser( config=> [@CONFIG] );
$gol->getoptions(\%opt_hash, keys %spec);
foreach (keys %spec) {
my $key = $_;
$key =~ s/[=\:\|\!]+.*//gs;
$opt_hash{$key} = $spec{$_} unless defined $opt_hash{$key}
}
# use Data::Dumper;
# print STDERR Dumper(\%opt_hash);
return %opt_hash;
}
### will do the equivalent of calling Getopt::Long::Configure;
sub Configure {
@CONFIG = @_;
}
1;
__END__
=pod
=head1 NAME
Getopt::Hash - wrapper for C to simply get command-line options
into a hash.
=head1 SYNOPSIS
use Getopt::Hash;
my %opt = GetOptions(
'option_name|on:s' => 'default_value',
'element|e!' => 0,
'count|cnt|c=i' => 5,
);
if ($opt{'option_name'} eq 'some_value') {
## do work ##
}
=head1 DESCRIPTION
Getopt::Hash wraps C in order to quickly get command-line options
into a local hash in one statement. Compare the L code above to the
equivalent code using C:
use Getopt::Long;
my %opt = (
'option_name' => 'default_value',
'element' => 0,
'count' => 5,
);
GetOptions(
'option_name|on:s' => \$opt{'option_name'},
'element|e!' => \$opt{'element'},
'count|cnt|c=i' => \$opt{'count'},
);
if ($opt{'option_name'} eq 'some_value') {
## do work ##
}
This presents a maintenance headache, as well as taking longer to initially
write.
=head1 EXPORTS
=over 1
=item C C<( %spec_hash )>
Returns a hash of command-line options as parsed by C.
C<%spec_hash> is of the form:
'getopt_spec_string' => 'default_value'
where C<'getopt_spec_string'> is an option spec string as expected by the
C module.
The hash returned by this subroutine uses the first form of each option as the
key, and the option's value (either as passed on the command-line, or the
default) as the value. For example:
my %opt = GetOptions(
'option_name|on:s' => 'default_value',
'element|e!' => 0,
'count|cnt|c=i' => 5,
);
when called without any command-line options, results in a hash C<%opt>:
'option_name' => 'default_value',
'element' => 0,
'count' => 5,
As with C's GetOptions subroutine, non-option arguments remain
in the C<@ARGV> array.
=back
=head1 CONFIGURATION
=over 1
=item C C<( @options )>
Sets the configuration options that will be passed to C's
Configure method during a call to C.
=back
=head1 HISTORY
=over 1
=item Version 0.81
Convert C to use C's internal method of populating
options to hashes.
=item Version 0.80
Initial release on PerlMonks (L)
=back
=head1 AUTHOR
Darren Meyer
=head1 COPYRIGHT AND DISCLAIMER OF WARRANTY
This module is available under the terms of the MIT License:
Copyright (c)2005 Darren Meyer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut