我公司使用Getopt::Declare
它作为命令行选项解析器.我们的选项处理块的结构通常如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
这是很多东西......我试着通过像大多数文档使用的那样使用HEREDOCS来使它变得更简单:
#!/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
Run Code Online (Sandbox Code Playgroud)
虽然我认为这更容易阅读,但由于某种原因它不会认出我的任何论点.
perl test.pl --client 5 --clicks 2
Run Code Online (Sandbox Code Playgroud)
我得到了无法识别的论点:
Error: unrecognizable argument ('--client')
Error: unrecognizable argument ('154')
Error: unrecognizable argument ('--clicks')
Error: unrecognizable argument ('2')
Run Code Online (Sandbox Code Playgroud)
所以我猜我有两个问题:
有没有人成功使用HEREDOCS和Getopt :: Declare?
是的Getopt ::声明仍是一个选项解析器一个合理的选择?与Getopt :: Long等其他模块相反
在原始版本中,您的字符串--clicks <N:i>
后跟一个选项卡,后跟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)
Run Code Online (Sandbox Code Playgroud)
>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)
Run Code Online (Sandbox Code Playgroud)
>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
>
Run Code Online (Sandbox Code Playgroud)