Can Perl Getopt :: Long Parse Argument I Do Not Define Before Time?

I know how to use Perl Getopt :: Long, but I'm not sure how I can configure it to accept any pair of “--key = value” that has not been explicitly defined and is hashed. In other words, I don’t know in advance what parameters the user might want, so I don’t need to define all of them, but I want them to be able to parse them.

Suggestions? Thanks in advance.

+4
source share
6 answers

The Getopt::Long documentation offers a configuration option that can help:

 pass_through (default: disabled) Options that are unknown, ambiguous or supplied with an invalid option value are passed through in @ARGV instead of being flagged as errors. This makes it possible to write wrapper scripts that process only part of the user supplied command line arguments, and pass the remaining options to some other program. 

After the regular parameters have been analyzed, you can use the code provided by runrig to analyze the special options.

+12
source

Getopt :: Long does not do this. You can analyze the parameters yourself ... for example.

 my %opt; my @OPTS = @ARGV; for ( @OPTS ) { if ( /^--(\w+)=(\w+)$/ ) { $opt{$1} = $2; shift @ARGV; } elsif ( /^--$/ ) { shift @ARGV; last; } } 

Either modify Getopt :: Long to process it (or modify the above code to handle more kinds of options if you need it).

+5
source

I am a bit incomplete, but I used Getopt :: In any case, to parse unknown arguments.

+2
source

You can potentially use the Options function with hash values . "

For example, I wanted to allow users to set arbitrary filters when analyzing an array of objects.

 GetOptions(my $options = {}, 'foo=s', 'filter=s%') my $filters = $options->{filter}; 

And then call it like

 perl ./script.pl --foo bar --filter baz=qux --filter hail=eris 

To build something like ..

 $options = { 'filter' => { 'hail' => 'eris', 'baz' => 'qux' }, 'foo' => 'bar' }; 

And of course, $ filters will have a value associated with 'filter'

Good luck Hope someone found this helpful.

+1
source

From the documentation :

Argument callback

The special option 'name' <> can be used to designate a routine for processing arguments without options. When GetOptions() encounters an argument that does not look like a parameter, it immediately calls this routine and passes it one parameter: the name of the argument.

Well, actually this is an object that builds the name of the argument.

For instance:

 my $width = 80; sub process { ... } GetOptions ('width=i' => \$width, '<>' => \&process); 

When applied to the following command line:

 arg1 --width=72 arg2 --width=60 arg3 

This will call process("arg1") , and $width is 80, process("arg2") , and $width is 72, and process("arg3") , and $width is 60.

For this function, you need to change the configuration parameter, see "Getopt :: Long Settings" section.

0
source

This is the right time to start your own parameter analyzer. None of the modules I've seen in CPAN provides this type of functionality, and you can always look at their implementations to get a good idea of ​​how to handle analysis nuts and bolts.

Aside, this type of code makes me hate Getopt options:

 use Getopt::Long; &GetOptions( 'name' => \$value ); 

Inconsistent capitalization belittles even people who have seen and used this style of code for a long time.

-1
source

All Articles