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.


In reply to Re: using "Getopt::Long" how to check parameter mandatory by TomDLux
in thread using "Getopt::Long" how to check parameter mandatory by shan_emails

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.