Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Range in Getopt

by Ammu (Novice)
on Oct 03, 2012 at 04:30 UTC ( #996976=perlquestion: print w/replies, xml ) Need Help??
Ammu has asked for the wisdom of the Perl Monks concerning the following question:

Hi i am using Getopt to accept the debug parameters.But it sholud be within the range 1-4.Can anyone tell me how to include the ange in Getopt?

Getopt::Long::GetOptions( 'd=i' => \$_debug) or usage("Invalid commmand line options.");

Replies are listed 'Best First'.
Re: Range in Getopt
by james2vegas (Chaplain) on Oct 03, 2012 at 06:16 UTC
    You can evaluate options with Getopt::Long using a custom subroutine, like this:

    use Getopt::Long; my $_debug; GetOptions('d=i' => sub { $_debug = $_[1]; ($_debug > 0 && $_debug <= 4) or die "Invalid Debug Level ($_d +ebug)" } ) and warn $_debug;

    GetOptions catches the die and throws it as a warning and returns false. When using the custom subroutine, you need to handle assigning to the variable for your option yourself.
      I like your solution, but when no -d parameter is present, the following warning is displayed:

      Warning: something's wrong at C:\tmp\ line 15.

      This appears to be a warning from Getopt::Long. I tried adding return 1 if !defined @_; at the top of the sub but the warning persists.

      "Its not how hard you work, its how much you get done."

        That is because while the d option's value is required, the d option itself is not, check the definedness of $_debug afterwards:

        use Getopt::Long; my $_debug; GetOptions('d=i' => sub { $_debug = $_[1]; ($_debug > 0 && $_debug <= 4) or die "Invalid Debug Level ($_d +ebug)" } ) or exit 1; die "Debug Level is required" unless defined $_debug; warn "Debug Level is $_debug\n";

        or set a default value before GetOptions:

        use Getopt::Long; my $_debug = 0; GetOptions('d=i' => sub { $_debug = $_[1]; ($_debug > 0 && $_debug <= 4) or die "Invalid Debug Level ($_d +ebug)" } ) or exit 1; warn "Debug Level is $_debug\n";
Re: Range in Getopt
by NetWallah (Abbot) on Oct 03, 2012 at 05:32 UTC
    One option is to use the "Specifying other parameter variable types" in Getopt::Declare.

    This allows declarative regex matching on the parameter.

                 I hope life isn't a big joke, because I don't get it.

Re: Range in Getopt
by dasgar (Curate) on Oct 03, 2012 at 06:48 UTC

    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.

Re: Range in Getopt
by Discipulus (Prior) on Oct 03, 2012 at 07:53 UTC
    i usually do like this:
    my $needed_param = 'need_default'; my $debug = 0; # default value numeric and not "" # getting params with getopt with a strict syntax # id est: if GetOptions fail may be better the program does NOT run at + all &help()if( ! GetOptions( 'needed=s' => \$needed_param, # [..] 'd=i' => \$_debug ) or ! $needed_param ); #then adjust values to a default or a maximum as needed $debug > 3 ? $debug = 3 : $debug ; sub help { "$0 -n [-d [0-3]] ... "}
    hope this help
    there are no rules, there are no thumbs..
Re: Range in Getopt
by toolic (Bishop) on Oct 03, 2012 at 13:54 UTC
    Another way (stealing james2vegas's die message):
    use warnings; use strict; use Getopt::Long; my $_debug = 1; # or whatever default you want it to be GetOptions('d=i' => \$_debug) or die("Invalid commmand line options.") +; die "Invalid Debug Level ($_debug)" if $_debug<1 or $_debug>4; print "\$_debug = $_debug\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996976]
Approved by Old_Gray_Bear
Front-paged by Corion
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-01-23 15:46 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (194 votes). Check out past polls.