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

时间:2019-11-30 02:31:39

标签: raku

我正在删除该帖子,因为没有接受有关审查/编辑的咨询。

2 个答案:

答案 0 :(得分:9)

以下是使用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]

答案 1 :(得分:8)

使用MAIN sub

#!/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>]

如果您只对带有单横线的参数感兴趣,请you'll need GetOpt::Long as indicated by Hakon