How can I allow undefined options when parsing args with Getopt

Posted by Ross Rogers on Stack Overflow See other posts from Stack Overflow or by Ross Rogers
Published on 2010-06-08T23:22:41Z Indexed on 2010/06/08 23:52 UTC
Read the original article Hit count: 300

Filed under:
|
|

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:

© Stack Overflow or respective owner

Related posts about perl

Related posts about getopt