Perl6:如何从命令行读取混合参数?



我一直从命令行运行程序,允许您混合参数的顺序。如果你把额外的东西扔进去,他们会抓住你。 例如:

$xxx -r abc  -q def  -w xyz
$xxx -w xyz  -q def  -r abc 

他们是怎么做到的? 是否有一些模块?

下面是一个使用 Getopt::Long 的示例:

use v6;
use Getopt::Long;
my %opt = help => False, 'r=s' => "", 'q=s' => "", 'w=s' => "";
my %options = get-options(%opt).hash;
say %options;
say @*ARGS;

示例运行:

$ p.p6  -w xyz -q def -r abc hello
{help => False, q => def, r => abc, w => xyz}
[hello]

使用MAIN子:

#!/usr/bin/env raku
use v6;
sub MAIN(:$these ="These", :$are="Are", :$params="Params") {
say "$these $are $params";
}

可以按任意顺序键入这些参数:

./command-line.p6 --are=well --these=those
those well Params

并且还将捕获任何额外的参数,向您显示实际参数:

./command-line.p6 --are=well --these=those --not=this_one
Usage:
./command-line.p6 [--these=<Any>] [--are=<Any>] [--params=<Any>]

如果您只对带有单个破折号的参数感兴趣,则需要 GetOpt::Long 如 Hakon 所示