Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Updated: Just learned that the special option 'name' "<>" can be used to designate a subroutine to handle non-option arguments (see Argument callback section in the docs). So the following is not entirely correct w.r.t. a "default option"...

Getopt::Long has lots of configuration options that have their own defaults, but there's no option to define a default option and default value/behavior for it. You need to do that for yourself, but Getopt::Long can help. One of its default behaviors is to flag as errors “Options that are unknown, ambiguous or supplied with an invalid option value” (see the pass_through configuration option).

So if you do nothing special, GetOptions() will tell you when you use an unknown option on the command line.

Using cjb's sample as a start, this shows what happens when you include an unknown option on the command line:

#!/usr/bin/env perl use 5.010; use strict; use warnings; use Getopt::Long; my $result = GetOptions ( h => \&help, d => sub {print "\nRun DEV COMPARE\n";}, p => \&PROD, ); say "result: $result"; bad_option() unless $result; say "\ncontinuing normally"; say "Doing stuff....."; say "exiting normally"; sub PROD {print "\nRun Production COMPARE\n";} sub help {print "\nHelp text\n";} sub bad_option { print "\nInvalid option detected... Quitting!!!\n"; exit 1; }
And run like so (only defined options):
./pm-888730 -h -d -p Help text Run DEV COMPARE Run Production COMPARE result: 1 continuing normally Doing stuff..... exiting normally

or thusly (one defined and one undefined option):

./pm-888730 -d -x Run DEV COMPARE Unknown option: x result: Invalid option detected... Quitting!!!

Comments:

  • GetOptions() returns 1 if all's well
  • default is a keyword starting in Perl 5.10, so I don't use it here as a sub name (though it is possible to define a sub named default, you just need to get the calling syntax right... exercise for the reader).
  • Note that the behavior here is for GetOptions() to service all valid options and also to flag the error(s). If you want to trap unknown options and quit immediately, you need to move your dispatch table outside the call to GetOptions() and trap the bad option(s) before dispatching.

In reply to Re^3: Parsing options with Getopt & Dispatch Table by broomduster
in thread Parsing options with Getopt & Dispatch Table by MKJ747

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found