2013-11-27 67 views
3

我的公司使用Getopt::Declare作为它的命令行选项解析器。我们的选择处理块的结构通常是这样的:Getopt :: Declare vs Getopt :: Long

Readonly my $ARGS => Getopt::Declare->new(
    join("\n", 
     "[strict]", 
     "--engineacct <num:i>\tEngineaccount [required]", 
     "--outfile <outfile:of>\tOutput file [required]", 
     "--clicks <N:i>\tselect keywords with more than N clicks [required]", 
     "--infile <infile:if>\tInput file [required]", 
     "--pretend\tThis option not yet implemented. " 
     . "If specified, the script will not execute.", 
     "[ mutex: --clicks --infile ]", 
    ) 
) || exit(1); 

这是很多来看看......我尝试用here文档最喜欢的文件,使之成为简单一些用途:

#!/usr/bin/perl 
use strict; 
use warnings FATAL => 'all'; 

use Readonly; 

Readonly my $ARGS => Getopt::Declare->new(<<'EOPARAM'); 
    [strict] 
    --client <client:i> client number [required] 
    --clicks <clicks:i> click threshold (must be > 5) 
EOPARAM 

虽然我觉得这样更容易阅读,但出于某种原因,它不会识别我的任何论点。

perl test.pl --client 5 --clicks 2 

我得到无法识别的参数:

Error: unrecognizable argument ('--client') 
Error: unrecognizable argument ('154') 
Error: unrecognizable argument ('--clicks') 
Error: unrecognizable argument ('2') 

所以我想我有两个quesitons:

  1. 已成功用于人用here文档Getopt的::声明

  2. Getopt :: Declare仍然是一个选项解析器的合理选项?相对于其他模块,如的Getopt ::龙

+0

的Getopt ::声明和的Getopt ::龙往往是最频繁使用的;我认为要么是一个可行的选择,取决于偏好。 –

回答

5

在原来的版本,你的字符串包含--clicks <N:i>后按Tab,然后select keywords with more than N clicks [required]

在您的修订版本中,您的字符串具有空格而不是选项卡。

使用<<"EOPARAM"和“\t”而不是<<'EOPARAM'和“”。

>type x.pl 
use Getopt::Declare; 
Getopt::Declare->new(<<'EOPARAM'); 
    [strict] 
    --client <client:i> client number [required] 
    --clicks <clicks:i> click threshold (must be > 5) 
EOPARAM 

>perl x.pl --client 5 --clicks 2 
Error: unrecognizable argument ('--client') 
Error: unrecognizable argument ('5') 
Error: unrecognizable argument ('--clicks') 
Error: unrecognizable argument ('2') 

(try 'x.pl -help' for more information) 

>type x.pl 
use Getopt::Declare; 
Getopt::Declare->new(<<'EOPARAM'); 
    [strict] 
    --client <client:i>\tclient number [required] 
    --clicks <clicks:i>\tclick threshold (must be > 5) 
EOPARAM 

>perl x.pl --client 5 --clicks 2 
Error: unrecognizable argument ('--client') 
Error: unrecognizable argument ('5') 
Error: unrecognizable argument ('--clicks') 
Error: unrecognizable argument ('2') 

(try 'x.pl -help' for more information) 

>type x.pl 
use Getopt::Declare; 
Getopt::Declare->new(<<"EOPARAM"); 
    [strict] 
    --client <client:i>\tclient number [required] 
    --clicks <clicks:i>\tclick threshold (must be > 5) 
EOPARAM 

>perl x.pl --client 5 --clicks 2 

> 
+0

即使删除了前导空格,它们仍然无法识别。 –

+0

是的,也发现了。更新。 – ikegami

+0

我很困惑,为什么该选项卡是必需的,但感谢您的帮助。 –