2012-04-02 104 views
-1

代码:奇怪的getopt = s处理?

#!/usr/bin/env perl 
use strict; 
use warnings; 
use locale; 

my $prepinac_r = ''; 
my $array_name = ''; 

use Getopt::Long; 
Getopt::Long::Configure ("bundling"); 
my $result = GetOptions(
"r=s" => \$prepinac_r, 
"array-name=s" => \$array_name); 

print STDERR "r: $prepinac_r\n"; 
print STDERR "array_name: $array_name\n"; 

运行它:

script.pl --array-name=kokos -r=kure 

输出:

r: =kure 
array_name: kokos 

我做错了吗?我错过了什么?为什么-r获得“= kure”而不是“kure”?请帮助...

+0

Getopt :: Long :: Configure(“bundling_override”); – rluks 2012-04-07 07:42:35

回答

4

您正在混合简短格式和长格式语法。简写形式的语法不会使用=,因为这会缩短文本的长度。

"a|all"  => \$opt_all, 
"e|execute=s" => \$opt_execute, 

简称:

-aefoo 
-a -efoo 
-a -e foo 

朗形式:

--all --execute=foo 
--all --execute foo 

这里是你可能熟悉的缩写形式的一个例子:

perl -le'print "Hello World";' 
perl -l -e'print "Hello World";' 
perl -l -e 'print "Hello World";' 
+0

如果'-x = x'或'x = x'''-e = x = x'表示允许'='的简写形式,那么将无法知道。 – ikegami 2012-04-04 07:38:44

1

学生先生,你应该使用双重-两个开关>>

script.pl --array-name=coconut --r=chicken 

...

r: chicken 
array_name: coconut 
+0

这会阻止他特别启用的捆绑。 – ikegami 2012-04-04 07:39:45

+0

是的,我也会这样做,但我对程序参数有严格的限制 – rluks 2012-04-07 07:41:52

0

我相信你所看到的行为是因为 “捆绑” 的。

Getopt::Long

Enabling this option will allow single-character options to be bundled. To distinguish bundles from long option names, long options must be introduced with -- and bundles with - 

因此,如果您使用的是“捆绑式”,然后 -

--array-name=foo --r=bar # Works 
-afoo -rbar    # Also works 

--array-name=foo -r=bar # Does not. as you've already seen 

它也没有意义的,使用绑定,除非你正在使用的选项,不需要参数,因此可以“捆绑”