user2361820
user2361820

Reputation: 449

Optional arguments in getopt

I'm trying to use getopt from Getopt::Std for multiple arguments. I have getopt('s:il'); where I want s to be a mandatory search word, i to be an optional integer, and l to be an optional letter. This is working when I use all 3, but if I add '-s search -i -l g' to my command line, I get the result -l in my variable for i, instead of Perl recognizing -i as blank and 'g' as an argument for l. Is there a way around this? Do I need to use an alternate getopt(s) command?

Upvotes: 1

Views: 454

Answers (1)

toolic
toolic

Reputation: 62037

Use the getopts function instead of getopt:

use warnings;
use strict;
use Getopt::Std;

my %opts;
getopts('s:il', \%opts);

use Data::Dumper;
$Data::Dumper::Sortkeys=1;
print Dumper(\%opts);

__END__

my_script.pl -s foo -i -l

$VAR1 = {
          'i' => 1,
          'l' => 1,
          's' => 'foo'
        };

Upvotes: 2

Related Questions