http://www.perlmonks.org?node_id=996993


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.