http://qs321.pair.com?node_id=893529


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.