• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

perl flags

How can I make my script understand flags?
as in

perl myscript.pl --flag1=0 --flag2=0

I would like to use flags instead of simple arguments because each of my arguments have defaults and if the user only wants to specify flag2, I don't want to force him/her to have to specify flag1 too as the default should be used.
0
NewtonianB
Asked:
NewtonianB
3 Solutions
 
BbouchCommented:
use Getopt::Long;
use Getopt::Long;
 
GetOptions      (
                "flag1=s" => \$myFlag1,
                "flag2=s"  => \$myFlag2,
                "help"    => \$myHelp
                );
 
 
if (defined $myHelp) {  displayHelpDialog; }
 
 
do { print "\nERROR: You must provide FLAG1 using the --flag1 option\n Use --help for usage info.\nProcess died"; exit(12); } unless defined $myFlag1;
 
 
do { print "\nERROR: You must provide FLAG2 using the --flag2 option\n Use --help for usage info.\nProcess died"; exit(12); } unless defined $myFlag2;

Open in new window

0
 
Adam314Commented:

use Getopt::Long;
 
my $flag1=1;  #Default for flag1
my $flag2=2;  #Default for flag2
 
GetOptions(
    "flag1=i" => \$flag1,
    "flag2=i" => \$flag2,
    ) or die "Could not process options\n";

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now