Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Range in Getopt

by dasgar (Curate)
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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2016-10-24 00:56 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.