perl - 无法解析命令行的长选项

标签 perl command-line-arguments getopt-long

#!/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-远程-测试

上面打印“0:0”。我是Perl的新手,所以我无法确定为什么它不起作用。

我还从http://perldoc.perl.org/Getopt/Long.html#Simple-options运行了“简单选项”部分,但也没有产生任何结果。

最佳答案

我相信,您在she-bang行中包含的-s命令行选项正在对您造成伤害。根据perlrun documentation-s命令行选项:

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



如果删除该选项,那么事情应该会按预期进行。我还建议您删除-w,因为您已经在使用use warnings指令(use warnings指令的功能更加强大,实际上是在替换-w选项)。

所以,长话短说,让您的第一行:
#!/usr/bin/perl

关于perl - 无法解析命令行的长选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077207/

相关文章:

Perl TK BrowseEntry ComboBox 默认选项

perl - 文档中的哪个地方说 while 测试 readdir 的定义?

python - 使用 argparse 处理不定的成对参数

python - 在Python中解析独占组

python - 在 Python 命令行参数中拆分长参数

c++ - 获取 optarg 作为 C++ 字符串对象

perl - 使用Getopt解析args时如何允许未定义的选项

perl - !~ 在 perl 中是什么意思

perl substr 获取子字符串

perl - 如果多次出现相同的选项,Getopt::Long GetOptions 是否会产生错误?