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

Re: Range in Getopt

by dasgar (Priest)
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]
[choroba]: Maybe the first four got downvoted and were deleted from the list?
LanX what?
marto deletes himself from the all lists
QM deletes himself from all delete lists

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-03-20 10:10 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (249 votes). Check out past polls.