Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I allow undefined options when parsing args with Getopt

If I have a command line like:

my_script.pl -foo -WHATEVER

My script knows about --foo, and I want Getopt to set variable $opt_foo, but I don't know anything about -WHATEVER. How can I tell Getopt to parse out the options that I've told it about, and then get the rest of the arguments in a string variable or a list?

An example:

use strict;
use warnings;

use Getopt::Long;

my $foo; 

GetOptions('foo' => \$foo); 

print 'remaining options: ', @ARGV;

Then, issuing

perl getopttest.pl -foo -WHATEVER

gives

Unknown option: whatever
remaining options:
like image 360
Ross Rogers Avatar asked Jun 08 '10 23:06

Ross Rogers


People also ask

What does the getopt function do?

The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and array as passed to the main() function on program invocation. An element of argv that starts with '-' (and is not exactly "-" or "--") is an option element.

How do I use getopt in Perl?

The getopts function takes two arguments: a string of options, and a hash reference. For each command line option (aka switch) found, getopts sets $opt{x} (where x is the switch name) to the value of the argument, or 1 if no argument was provided.

What is Optarg getopt?

optarg indicates an optional parameter to a command line option. opterr can be set to 0 to prevent getopt() from printing error messages. optind is the index of the next element of the argument list to be process, and optopt is the command line option last matched.


2 Answers

You need to configure "pass_through" option via Getopt::Long::Configure("pass_through");

Then it support actual options (e.g. stuff starting with "-" and without the special "--" delimiter to signify the end of "real" options).

Here's perldoc quote:

  • 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.

Here's an example

$ cat my_script.pl
#!/usr/local/bin/perl5.8 -w

use Getopt::Long;
Getopt::Long::Configure("pass_through");
use Data::Dumper;
my %args;
GetOptions(\%args, "foo") or die "GetOption returned 0\n";
print Data::Dumper->Dump([\@ARGV],["ARGV"]);

$ ./my_script.pl -foo -WHATEVER          
$ARGV = [
          '-WHATEVER'
        ];
like image 195
DVK Avatar answered Sep 21 '22 18:09

DVK


Aren't the remaining (unparsed) values simply left behind in @ARGV? If your extra content starts with dashes, you will need to indicate the end of the options list with a --:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;

my $foo;
my $result = GetOptions ("foo"   => \$foo);
print Dumper([ $foo, \@ARGV ]);

Then calling:

my_script.pl --foo -- --WHATEVER

gives:

$VAR1 = [
          1,
          [
            '--WHATEVER'
          ]
        ];

PS. In MooseX::Getopt, the "remaining" options from the command line are put into the extra_argv attribute as an arrayref -- so I'd recommend converting!

like image 26
Ether Avatar answered Sep 17 '22 18:09

Ether