2013-11-27 69 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

我很困惑,爲什麼該選項卡是必需的,但感謝您的幫助。 –