in reply to using "Getopt::Long" how to check parameter mandatory
I'm a Getopt::Long fan. Rather than using separate variables, I use the option to put all the arguments into a hash. As well, I integrate with documentation using Pod::Usage. Initializing the options hash provides default values. '-help' displays a brief summary, while '-man' displays the full POD.
my %options = ( date => prev_day( strftime( '%Y%m%d', localtime + )), qadir => $QADIR, proddir => $PRODDIR, ); GetOptions( \%options, 'date=s', 'qadir=s', 'proddir=s', 'exch=s', 'altexch=s', 'man', 'help', 'alert!', 'debug+' ) || pod2usage(2); pod2usage(1) if $options{'help'}; pod2usage( '-verbose' => 2 ) if $options{'man'};
To handle mandatory arguments, obviously there's no default. Instead, I test for a value. If '-file' has not been provided, I print the message followed by '-help' summary, and exit with status '1'. The only pain is testing all the requirements and inter-correlations between arguments and coming up with the correct set of error messages. It's very irritating to get an error message for a program, provide the requested argument, and get a next error message.
pod2usage( { -message => q{Mandatory argument '-file' is missing} +, -exitval => 1 , -verbose => 1 } ) unless $options{file};
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
|
|---|