Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-10-24 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?