There are many Perl tutorials explaining how to use GetOptions utility to process only the command-line arguments which are expected, else exit with an appropriate message.
In my requirement I have following optional command-line arguments, like,
I tried few combinations with GetOptions which did not work for me.
So my question is: How to use GetOptions to handle this requirement?
EDIT: -z needs 'zip directory path'
EDIT2: My script has following compulsory command-line arguments:
Here's my code:
my %args;
GetOptions(\%args,
"in=s",
"out=s"
) or die &usage();
die "Missing -in!" unless $args{in};
die "Missing -out!" unless $args{out};
Hope this EDIT adds more clarity.
A :
(colon) can be used to indicate optional options:
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
my ( $zip, $help, $input_dir, $output_dir );
GetOptions(
'z:s' => \$zip,
'h' => \$help,
'in=s' => \$input_dir,
'out=s' => \$output_dir,
);
From the documentation:
: type [ desttype ]
Like "=", but designates the argument as optional. If omitted, an
empty string will be assigned to string values options, and the
value zero to numeric options.
If you specify that and check for the empty string, you know which ones the user did not specify.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With