Reputation: 8061
Scenario: I need to pass names of multiple tv shows to a script:
Eg. ./script -a "Homeworld" -a "Supernatural"
I'm using Getopt::Long::Configure to get the options.
sub ArgParser
{
my ($help,$addshow,$delshow,$checkshow,$listshows) = ();
GetOptions ('help|h' => \$help,
'add|a=s' => \$addshow,
);
if ($help)
{
HelpPrint;
}
elsif ($addshow)
{
say $addshow;
}
else
{
HelpPrint("Invalid option or no options specified!");
}
exit;
}
ArgParser;
Currently, only the last specified argument is being received by the script. How can I detect whether multiple parameters to the same argument are being passed on the command line?
@ARGV does contain all arguments, so how can I use them?
Upvotes: 1
Views: 13461
Reputation: 433
You could just pass the argument with a separator:
Often it is useful to allow comma-separated lists of values as well as multiple occurrences of the options. This is easy using Perl's split() and join() operators:
GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(',',@libfiles));
Or another option is:
--library lib/stdlib --library lib/extlib
To accomplish this behaviour, simply specify an array reference as the destination for the option:
GetOptions ("library=s" => \@libfiles);
Using the first example, your command line would be:
./script -a "Homeworld" -a "Supernatural" -a "Breaking Bad"
-or-
./script -a "Homeworld,Supernatural,Breaking Bad"
-or-
./script -a "Homeworld,Supernatural" -a "Breaking Bad"
Using the second example, your command line would be:
./script -a "Homeworld" -a "Supernatural" -a "Expanse, The"
Upvotes: 6