在较新的Getopt :: Long中如何设置默认的可选值

roc*_*cky 3 perl getopt-long

在Perl的Getopt :: Long版本2.39我可以使用

use Getopt::Long qw( :config gnu_getopt );
GetOptions(
   \my %opts, 
   "codon-view|c:20",    # Optional value, default 20
   "consensus|C:50", 
   ...
)
Run Code Online (Sandbox Code Playgroud)

表示,如果我使用-c默认值是20放在%opts下键codon-view-c给出,但它没有明确的价值是存在的.另一方面-c或未--codon-view提供,则哈希表中没有值存储%opts.

在2.48中,这不再有效,我在Getopt :: Long的文档中没有看到

$ perl -E'
   use Getopt::Long qw( :config gnu_getopt );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.39
20

$ perl -E'
   use Getopt::Long qw( :config gnu_getopt );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.48
[undef]
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现旧的行为?

救命!

ike*_*ami 5

这是2.48中引入的变化.

$ perl -E'
   use Getopt::Long qw( :config gnu_getopt );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.47
20

$ perl -E'
   use Getopt::Long qw( :config gnu_getopt );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.48
[undef]
Run Code Online (Sandbox Code Playgroud)

我不确定,但我认为这是无意中完成的,所以我提交了一份错误报告.


use Getopt::Long qw( :config gnu_getopt );
Run Code Online (Sandbox Code Playgroud)

是的缩写

use Getopt::Long qw( :config gnu_compat bundling permute no_getopt_compat );
Run Code Online (Sandbox Code Playgroud)

你是如何投入使用的gnu_compat

$ perl -E'
   use Getopt::Long qw( :config gnu_getopt );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.48
[undef]

$ perl -E'
   use Getopt::Long qw( :config gnu_compat bundling permute no_getopt_compat );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.48
[undef]

$ perl -E'
   use Getopt::Long qw( :config bundling permute no_getopt_compat );
   say $Getopt::Long::VERSION;
   GetOptions(\my %opts, "codon-view|c:20");
   say $opts{"codon-view"} // "[undef]"
' -- -c
2.48
20
Run Code Online (Sandbox Code Playgroud)

gnu_compat控制是否--opt=允许,以及它应该做什么.没有gnu_compat,--opt=给出错误.随着gnu_compat,--opt=将给出选项opt和空值.这是GNU getopt_long()的方式.

因此,如果你可以--codon-view=分配零$opts{"codon-view"},只需使用

use Getopt::Long qw( :config bundling permute no_getopt_compat );
Run Code Online (Sandbox Code Playgroud)

代替

use Getopt::Long qw( :config gnu_getopt );
Run Code Online (Sandbox Code Playgroud)