Unable to parse command line long options
Asked Answered
E

2

6
#!/usr/bin/perl -sw
use strict;
use warnings;
use Getopt::Long;

my $remote = 0;
my $test = 0;
GetOptions ('remote' => \$remote, 'test' => \$test);
print "$remote:$test\n";

perl test.pl --remote --test

The above prints "0:0". I am new to Perl so I have been unable to determine why this isn't working.

I also ran the "Simple Options" section from http://perldoc.perl.org/Getopt/Long.html#Simple-options and that didn't produce anything either.

Endymion answered 22/8, 2012 at 16:9 Comment(1)
@raina77ow - I did have them, just didn't include in this post.Endymion
S
10

I believe the -s command line option you include on your she-bang line is biting you. According to the perlrun documentation, the -s command line option:

enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of --).

If you remove that option, things should work as you expect. I would also recommend removing the -w since you are already using the use warnings directive (the use warnings directive is much more fully featured, essentially replacing the -w option).

So, long story short, make your first line:

#!/usr/bin/perl
Stanfordstang answered 22/8, 2012 at 16:23 Comment(1)
Thanks, I was using that for short commands earlier but wanted to replace those (since I was using full words anyway.)Endymion
A
0

Note that if running the script on Windows via cmd you must specify perl before the script name otherwise GetOptions doesn't work.

When I tried simply calling my script.pl on the command line without first putting perl the script ran but all the options weren't parsed.

Ambidexterity answered 1/11, 2018 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.