下面是一个使用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)
使用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 所示