Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^3: Dynamic option

by thargas (Deacon)
on Mar 16, 2011 at 11:35 UTC ( #893529=note: print w/replies, xml ) Need Help??

in reply to Re^2: Dynamic option
in thread Dynamic option

using Getopt::Long, you *can* do this, but it's not going to be pretty as you're going to have to wedge your code into it. As some others have suggested, read the complete docs for Getopt::Long; there's a lot in there you probably don't know about.

For each defined option, you can supply a reference. If it's a scalar, array or hash ref, it's used as the place to store the value(s) for that option, but if it's code, it gets run. Using that, you can insert your own code into the option processing and do whatever you need. At least I've been able to do anything I've needed.

For example: (untested)

my $opt = { grot => 'default for grot', }; my $action = 'default action'; GetOptions( $opt, 'action=s' => sub { my ($name, $value) = @_; $action = $value; }, 'thing=s' => sub { my ($name, $value) = @_; $opt->{$value} = $action; }, ) or die "can't parse command-line:";

And then you can invoke it as:

./myscript --action frob --thing ant --thing bee --action foo --thing cat

I'm sure it's not exactly what you wanted, but then I'm not sure what you wanted except that this seems relevant for you to build it.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2023-09-25 15:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found