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 (optional) 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. Placed here as not to clobber other actions. # This example uses a callback to validate that user accounts don't already exist. $parser->add_option( '-username', { dest => 'username', action => 'append', help => 'Username for new ILO account', callback => sub { my ($parser, $options) = @_; for my $uname (@{$options->{username}}) { if ($uname) { my $code = system(sprintf("getent passwd %s 2>&1 > /dev/null", $uname)); if (! $code) { printf("Error: -username provided already exists: %s\n", $uname); exit 1; } } else { printf("Error: -username provided not defined: %s\n", $uname); exit 2; } } } } ); # This example uses a callback to ensure a hostname is resolvable. $parser->add_option( '-hostname', { dest => 'hostname', help => 'Remote hostname', default => 'cpan.perl.org', callback => sub { my ($parser, $options) = @_; my $hostname = $options->{hostname}; if ($hostname) { if ($hostname =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) { printf("Error: -hostname should be resolvable fqdn not IP: %s\n", $hostname); exit 3; } if (! gethostbyname($hostname)) { printf("Error: unable to resolve -hostname: %s\n", $hostname); exit 4; } } } } ); # This example uses a callback to validate password integrity. $parser->add_option( '-password', { dest => 'password', help => 'Password for account', callback => sub { my ($parser, $options) = @_; my $password = $options->{password}; if ($password) { if ($password !~ /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])/s || (length($options->{password}) < 10)) { print "Error: Password should be at least 10 characters, contain numbers and a lower and upper case letters.\n"; exit 5; } } } } ); 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