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

Tod*_*odd 9 raku

我正在删除帖子,因为我没有就审查/编辑征询我的意见。

Håk*_*and 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;
Run Code Online (Sandbox Code Playgroud)

示例运行:

$ p.p6  -w xyz -q def -r abc hello
{help => False, q => def, r => abc, w => xyz}
[hello]
Run Code Online (Sandbox Code Playgroud)


jjm*_*elo 8

使用MAIN子:

#!/usr/bin/env raku

use v6;

sub MAIN(:$these ="These", :$are="Are", :$params="Params") {
    say "$these $are $params";
}
Run Code Online (Sandbox Code Playgroud)

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

./command-line.p6 --are=well --these=those
those well Params
Run Code Online (Sandbox Code Playgroud)

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

./command-line.p6 --are=well --these=those --not=this_one
Usage:
  ./command-line.p6 [--these=<Any>] [--are=<Any>] [--params=<Any>]
Run Code Online (Sandbox Code Playgroud)

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