Perl - How To Use Getopt - Long Method - Stack Overflow PDF
Perl - How To Use Getopt - Long Method - Stack Overflow PDF
log in
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
tour
help
How can I use Getopt::Long method if the input command execution is like this:
$ testcmd -option check
or
$ testcmd
perl
getopt-long
That depends on what you want to have done with those options and flags. The people who answer below are
trying to make their best guess, but mind-reading is a difficult skill to master. Ether Jun 30 '10 at 16:15
add a comment
3 Answers
A quick example:
#! /usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
sub usage { "Usage: $0 [--option=VALUE] ARG1 ARG2 ARG3\n" }
my $option = "default";
GetOptions("option=s", \$option)
or die usage;
die usage unless @ARGV == 3;
print "$0: option=$option: @ARGV\n";
Getopt::Long is quite flexible in what it will accept:
http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method
1/3
11/11/2014
add a comment
+1 for recognizing that values starting with "-" need to be passed through DVK Jun 30 '10 at 13:28
add a comment
http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method
2/3
11/11/2014
"one=s"
"two=s"
"three=s"
"help|h|?"
=>
=>
=>
=>
\$one,
\$two,
\$three,
\$help,
) or usage ();
if ($help) {
usage ();
}
my $first = $one;
my $second = $two;
my $third = $three;
printf ("%-7s %-9s %-7s\n", $first, $second, $third);
sub usage {
print "\n$0\t" . "[ -one <text> -two <text> -three <text> ]\n\n";
exit (0);
}
answered Nov 3 '13 at 2:52
Rukmangathan
1
Maybe you should add some sort of explanation to your code so it can be more useful. Meryovi Nov 3 '13 at
14:25
add a comment
Not the answer you're looking for? Browse other questions tagged perl getopt-long or
ask your own question.
http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method
3/3