如何在子主机中获得强制性命名参数?

发布于 2025-02-08 04:24:41 字数 290 浏览 0 评论 0 原文

这是我能得到的最好的:

sub MAIN(Int :p($parm)!)
{
  say "* parm=", $parm;
}

但是:

$ raku test-par.raku
Usage:
  test-par.raku -p[=Int]

它说参数是可选的!
确实是:

 $ raku test-par.raku -p
 * parm=True

那么,什么给了?

This is the best I could get:

sub MAIN(Int :p($parm)!)
{
  say "* parm=", $parm;
}

But:

$ raku test-par.raku
Usage:
  test-par.raku -p[=Int]

It says the parameter is optional!
And indeed it is:

 $ raku test-par.raku -p
 * parm=True

So, what gives ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2025-02-15 04:24:41

bool 也恰好是 int

$ raku -e 'say True.Int'
1

$ raku -e 'say True ~~ Int'
True

因为 -p bool 它也是 int

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' -p=42
IntStr.new(42, "42")
True
42

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' -p
Bool::True
True
1

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' --/p
Bool::False
True
0

该参数仍然是可选的; -p 刚刚(可以说是不直觉)的处理,例如 -p = 1 。不幸的是,要实际执行所请求的约束将需要添加一个额外的过滤器:

$ raku -e 'sub MAIN(Int :p($param) where * !~~ Bool) { say $param.raku; say $param ~~ Int; say $param.Int }' --p=1
IntStr.new(1, "1")
True
1

$ raku -e 'sub MAIN(Int :p($param) where * !~~ Bool) { say $param.raku; say $param ~~ Int; say $param.Int }' -p
Usage:
  -e '...' [-p[=Int where { ... }]]

A Bool also happens to be an Int:

$ raku -e 'say True.Int'
1

$ raku -e 'say True ~~ Int'
True

Because -p is a Bool it is also an Int:

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' -p=42
IntStr.new(42, "42")
True
42

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' -p
Bool::True
True
1

$ raku -e 'sub MAIN(Int :p($param)) { say $param.raku; say $param ~~ Int; say $param.Int }' --/p
Bool::False
True
0

The parameter is still optional; -p is just being treated (arguably unintuitively) like -p=1. To actually enforce the requested constraint would unfortunately require adding an additional filter:

$ raku -e 'sub MAIN(Int :p($param) where * !~~ Bool) { say $param.raku; say $param ~~ Int; say $param.Int }' --p=1
IntStr.new(1, "1")
True
1

$ raku -e 'sub MAIN(Int :p($param) where * !~~ Bool) { say $param.raku; say $param ~~ Int; say $param.Int }' -p
Usage:
  -e '...' [-p[=Int where { ... }]]
清风不识月 2025-02-15 04:24:41

正如其他一些问题所述 true enum ,值 1 ,所以它是 int ,如果要确保始终以一个值调用 -p 您可以使用 args to to to to to 功能在之前检查传入的参数 它们被传递给main。类似:

sub ARGS-TO-CAPTURE(&main, @args) {
    if none(@args) ~~ /"-p=" \d+/ { 
        say $*USAGE; 
        exit;
    } 
    &*ARGS-TO-CAPTURE(&main,@args)
}

这里我们检查至少一个参数是 -p ,并且它也具有整数值。您还可以通过添加生成 - 使用如果要在输出中更改其显示 -p 的方式。

As some other questions have said True is an Enum with value of 1 so it's and Int if you want to ensure the -p is always called with a value you can use the ARGS-TO-CAPTURE function to check the incoming arguments before they are passed to main. Something like :

sub ARGS-TO-CAPTURE(&main, @args) {
    if none(@args) ~~ /"-p=" \d+/ { 
        say $*USAGE; 
        exit;
    } 
    &*ARGS-TO-CAPTURE(&main,@args)
}

Here we check that at least one of the arguments is -p and it has also got an integer value. You can also override the $*USAGE value by adding the GENERATE-USAGE sub if you want to change how it displays -p in the output.

老街孤人 2025-02-15 04:24:41

首先,感谢您的答案和评论。

对于我的特定异想天开,这是我可以做到的最好的:

use v6;

use Getopt::Long;

get-options("p|port=i" => my $port);

sub MAIN
{
  # for the case that no args were passed
  without $port { say "*** error, missing args"; exit }

  say "*** port=$port"
}

据我所知,它可以处理简短和长期的选择,并使选项必须进行。

First of all, thanks for the answers and comments.

For my specific whims, this is the best I could come to:

use v6;

use Getopt::Long;

get-options("p|port=i" => my $port);

sub MAIN
{
  # for the case that no args were passed
  without $port { say "*** error, missing args"; exit }

  say "*** port=$port"
}

As far as I can see, it handles short and long options, and makes the options mandatory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文