I have several mutually exclusive flags witch have their own options. Lets say, if I invoke the "stop_service" flag, I want a "name" option; but if I invoke the "send_report" flag I want a "email" option. For parsing that I use "Getopt::Long". This is the code:
use Getopt::Long;
# Option vars
my $stop_service; # flag
my $send_report; # flag
my $name; # string
my $email; # string
# Get all possible options
GetOptions(
# Flag and options for stop_service
"stop_service" => \$stop_service, # Mutual Exclusion Flag
"name=s" => \$name, # option string
# Flag and options for send_report
"send_report" => \$send_report, # Mutual Exclusion Flag
"email=s" => \$email, # option string
);
# Parsing correct combinations
# --stop_service --name XXX
if (($stop_service and !$send_report) # mutual exclusion
and ($name && !$email)) # options
{
print "stop_service + name: \n";
print $stop_service, " - ", $name, "\n";
}
# --send_report --email XXX
elsif ((!$stop_service and $send_report) # mutual exclusion
and (!$name && $email)) # options
{
print "send_report + email: \n";
print $send_report, " - ", $email, "\n";
}
# HELP
else {
print <<DOC;
Help in line 1.
Help in line 2.
DOC
}
It works well:
[getopt]$ perl 06_getopt_cond_3.pl --stop_service --name jumersindo
stop_service + name:
1 - jumersindo
[getopt]$ perl 06_getopt_cond_3.pl --send_report --email jumer@jum.er
send_report + email:
1 - jumer@jum.er
[getopt]$ perl 06_getopt_cond_3.pl --send_report --name
Option name requires an argument
Help in line 1.
Help in line 2.
Are there a more "automatic" way of configuring that? or I need to specify all the option combinations with the "if" sentences?