NAME
Getopt::optparse - optparse style processing of command line options
This library supports both single and double dash options. An equal sign must be used.
SYNOPSIS
use Getopt::optparse;
my $parser = Getopt::optparse->new();
$parser->add_option(
'--hostname',
{
dest => 'hostname',
help => 'Remote hostname',
default => 'localhost.localdomain'
}
);
$parser->add_option(
'--global', {
dest => 'global',
action => 'store_true',
help => 'Show global',
default => 0
}
);
$parser->add_option(
'--username',
{
dest => 'username',
action => 'append',
help => 'Usernames to analyze'
}
);
$parser->add_option(
'-v',
{
dest => 'verbose',
action => 'count',
help => 'Increment verbosity'
}
);
my $options = $parser->parse_args();
printf("Hostname is: %s\n", $options->{hostname});
printf("Username is: %s\n", $options->{username});
if ($options->{global}) {
}
for my $uname (@{$options->{username}}) {
print $uname, "\n";
}
DESCRIPTION
Library which allows Python optparse style processing of command line options.
CONSTRUCTOR
- $parser = Getopt::optparse->new( \%options )
-
Construct a new
Getopt::optparse
object and return it. Hash reference argument may be provided though none are required.
METHODS
The following methods are available:
- Getopt::optparse->add_option( 'optionname', {option_attributes} )
-
Add option to be parsed from command line. Accepts two arguments. Both are required:
$parser->add_option( '--hostname', { dest => 'hostname', help => 'Remote hostname', default => 'localhost.localdomain' } )
- Option Name (required)
-
Value to be parsed from command line. --hostname in the above example. This library supports both single and double dash option names..
- Option Attributes hash reference (required)
-
These may include:
- dest (required)
-
Name of key were parsed option will be stored.
- default (optional)
-
Value of dest if no option is parsed on command line.
- help (optional)
-
Text message displayed when -h or --help is found on command line.
- action (optional)
-
The following actions are supported.
- store_true
-
Using this makes dest true or false (0 or 1) if the option name is found on the command line.
- append
-
Using this appends each occurrance of an option to an array reference if option name is found on the command line.
- count
-
Using this increments dest by one for every occurrence if option name is found on the command line.
- callback
-
Allows user to pass code reference which is executed after Getopt::optparse->parse_args() is run. The callback has access to to all parsed options from command line.
$parser->add_option( '--password', { dest => 'password', help => 'Password for account', action => 'callback', callback => sub { my ($parser, $options) = @_; if ($options->{password}) { if ($options->{password} !~ /(?=[0-9])(?=[A-Z])(?=[a-z])/) { if (length($options->{password}) < 10) { print "Error: Password should be at least 10 characters, contain numbers and a lower and upper case letters.\n"; exit 2; } } } } } );
- Getopt::optparse->parse_args()
-
Parse added options from command line and return their values as a hash reference.
my $options = $parser->parse_args(); printf("Hostname is: %s\n", $options->{hostname}); for my $uname (@{$options->{username}}) { print $uname, "\n"; }
AUTHOR
Matt Hersant <matt_hersant@yahoo.com>