Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: -x accepts up to 3 additional arguments

by Xaositect (Friar)
on Jun 23, 2005 at 17:29 UTC ( #469464=note: print w/replies, xml ) Need Help??

in reply to -x accepts up to 3 additional arguments

Yeah, the Getopt::Long convention for switches with multiple arguments would be to specify the same switch multiple times on the call, like: -x opt1 -x opt2 -x opt3

Since it seems that's not an option, (apologies for the pun) you can still use Getopt::Long, but I think you'll need to do something a little less nice. Maybe like:

my @x; my @y; my $z; GetOptions( #three options required 'x=s' => sub { shift(); push(@x, shift, shift(@ARGV), shift(@ARGV)); + }, #two options required 'y=s' => sub { shift(); push(@y, shift, shift(@ARGV)); }, 'z=s' => \$z) or die ("Invalid arguments");

This reaching into @ARGV has the obvious side effect of potentially breaking further option processing if the number of options isn't exactly what you expect. You could easily add you own logic to the processing functions though. (The initial shift discards the first parameter, which is the option itself (-x, -y, etc))

Xaositect -

Replies are listed 'Best First'.
Re^2: -x accepts up to 3 additional arguments
by Codon (Friar) on Jun 23, 2005 at 19:22 UTC

    I think this is the closest to a "best solution" compared to some the others listed. Define a callback handler for your options such as:

    my $flag, @args; my %more = ( x => 2, y => 1, z => 0 ); GetOptions( 'x=s' => \&parse_opt, 'y=s' => \&parse_opt, 'z=s' => \&parse_opt, ); sub parse_opt { my ($opt, $val) = @_; # splice additional args off @ARGV based on how many I expect push @args, $val, splice @ARGV, 0, $more{$opt}, (); }

    Of course, all of this is going to depend on what you want to do with things later and on whether then number of params that -x and -y take varies, etc.

    Ivan Heffner
    Sr. Software Engineer, DAS Lead, Inc.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469464]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-10-23 00:00 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (232 votes). Check out past polls.