Reputation: 35
When I use the "one_of" special specification in Getopt::Long::Descriptive, I get errors for the options I did not put on the command line. I would like to have Getopt::Long::Descriptive handle the mutually exclusive options for me; but I don't know how to use the specification without generating errors.
If I list the options without including them in a one_of specifier, they do work as expected. However, I can then put multiple options on the command line which should be mutually exclusive. Perl 5.26.1 and Getopt::Long::Descriptive 0.104 if it helps.
use Getopt::Long::Descriptive;
my $usage_desc = "%c %o mailbox|ticket <This script lists or adds stuff>";
my @opt_spec = (
[
"mode" => hidden => {
one_of => [
[ 'list|l=s' => 'lists stuff' ],
[ 'add|a=s' => 'adds stuff' ],
]
}
],
[ 'verbose|v' => 'increase the verbosity level in the log file' ],
[ 'help' => 'print usage message and exit', { shortcircuit => 1 } ],
);
my ( $opt, $usage ) = describe_options( $usage_desc, @opt_spec );
print( $usage->text ), exit if $opt->help;
if ( $opt->verbose ) {
$debug_level++;
print "option --verbose specified; debug level now at $debug_level\n";
}
if ( $opt->list ) {
my $argument = $opt->list;
print "option --list specified, with argument: $argument\n";
}
if ( $opt->add ) {
my $argument = $opt->add;
print "option --add specified, with argument: $argument\n";
}
foreach my $argument ( @ARGV ) {
print "argument '$argument\' supplied (without an option specified)\n";
handle_search_terms( $argument );
}
When I run my script, for example with --list foo, then I get an error
"Can't locate object method "add" via package "Getopt::Long::Descriptive::Opts::__OPT__::1" at line 38."
If I run it with --add bar, then I get the error "Can't locate object method "list" via package...."
My problem is that I don't know how to structure my code so that the options not used don't generate errors.
Upvotes: 1
Views: 169
Reputation: 687
if ( $opt->mode eq 'list' ) {
my $argument = $opt->list;
print "option --list specified, with argument: $argument\n";
}
elsif ( $opt->mode eq 'add' ) {
my $argument = $opt->add;
print "option --add specified, with argument: $argument\n";
}
And don't forget:
use strict;
use warnings;
All of this is described in the documentation.
Upvotes: 1