Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to parse command line long options

#!/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.

like image 618
scott.smart Avatar asked Aug 22 '12 16:08

scott.smart


People also ask

How do I parse bash script arguments?

We can use the getopts program/ command to parse the arguments passed to the script in the command line/ terminal by using loops and switch-case statements. Using getopts, we can assign the positional arguments/ parameters from the command line to the bash variables directly.

What does Getopt do in C?

The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option.

How do I pass a command line argument in Scala?

The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings.

How do you access command line arguments from within a shell script?

The special character $# stores the total number of arguments. We also have $@ and $* as wildcard characters which are used to denote all the arguments. We use $$ to find the process ID of the current shell script, while $? can be used to print the exit code for our script.


1 Answers

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
like image 178
Jonah Bishop Avatar answered Sep 20 '22 18:09

Jonah Bishop