Hello Perlmonks, I have the following code and I am wondering if there is a way to actually validate the options passed BEFORE processing? As you can see based upon the execution of the code, Getopt is processing the arguments as they are received. The value of --year was passed which is not a valid argument, but then it processed --first successfully. What I would like to accomplish is if any invalid argument is passed, immediatly display the help and exit the script without processing any arguments

#!/usr/bin/env perl use warnings; use strict; use Getopt::Long; GetOptions( 'first' => \&first, 'second' => \&second, 'help' => \&help, ) or die "Invalid options passed to $0\n"; sub first () { print "Processing sub first\n"; } sub second () { print "Processing sub second\n"; } sub help () { print "Available options are: \n"; print "\t--first\n"; print "\t--second\n"; print "\t--third\n"; print "\t--fourth\n"; print "\t--help\n"; }
***** Output ******* C:\temp>getopts.pl --year --first Unknown option: year Processing sub first Invalid options passed to C:\temp\getopts.pl

In reply to Getopt - Validate arguments before processing by g_speran

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.