js1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have the following code in my script test.pl which gets command line options.

getopts("ciphdvs:",\%opts); ## help if($opts{h}){

I can run the script without any options, but if it's run with an option that's not available, e.g. 'test.pl -b' I want to print the help message. How can I test for an undefined option?

Thanks for any help.

Replies are listed 'Best First'.
Re: test for unknown option in getopts
by gellyfish (Monsignor) on Aug 18, 2006 at 09:43 UTC

    Just test the return value of the getopts :

    use Getopt::Std; + my %opts; + getopts("ciphdvs:",\%opts) or die "$0: USAGE: blah blah\n";;

    /J\

      The OP should note that that will result in an error line printed to stderr with text of the form: "Unknown option: b".


      DWIM is Perl's answer to Gödel
      Thanks - that did it!