Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2017-08-24 05:03 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (364 votes). Check out past polls.