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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |