Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Range in Getopt

by dasgar (Deacon)
on Oct 03, 2012 at 06:48 UTC ( #996993=note: print w/ replies, xml ) Need Help??


in reply to Range in Getopt

Here's my typical approach to using Getopt::Long. First I assign a default value to the variables that will be used in GetOptions(). Then I test the values of those variables. If they are acceptable values, proceed. Otherwise, stop and give an error message.

Applied to your question, here's what my (untested) code would look like:

use strict; use warnings; use Getopt::Long; my $debug = ""; GetOptions( 'd=i' => \$debug); if ($debug eq "") { # -d flag was not used } else { if (($debug < 1) && ($debug > 4)) { # -d flag used, but value is not acceptable my $msg = "The value of '$debug' is not valid for -d option.\n"; $msg = "The -d option only accepts the values between 1 and 4.\n" die "$msg"; } }

Might not be the best or more elegant solution, but it should work and it makes sense to me.


Comment on Re: Range in Getopt
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-08-31 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls