多个 MAIN 签名 [英] Multiple MAIN signatures
问题描述
我有一个包含多个 main 的包,我想定义几个选项:
I have a package with multiple main and I want to define several options:
我的代码是这样的:
package Perl6::Documentable::CLI {
proto MAIN(|) is export {*}
my %*SUB-MAIN-OPTS = :named-everywhere;
multi MAIN(
"setup"
) { ... }
multi MAIN (
"start" ,
Str :$topdir = "doc",
Bool :v(:verbose($v)) = False
) { ... }
但是当我尝试实际执行它时:
But when I try to actually execute it with:
perl6 -Ilib bin/documentable start -v --topdir=ss
它输出这一行:
Usage:
bin/documentable [--topdir=<Str>] [-v|--verbose] start
我正在使用 %*SUB-MAIN-OPTS 但它看起来也不起作用.
I am using %*SUB-MAIN-OPTS but it looks like it does not work neither.
推荐答案
最简单的解决方案是导出动态变量 %*SUB-MAIN-OPTS
,但这仍然没有完全实现:导出工作正常,但最终成为一个空哈希.所以不是很有用.
The simplest solution would be to export the dynamic variable %*SUB-MAIN-OPTS
, but that is still Not Yet Implemented completely: the export works sorta, but winds up being an empty hash. So not very useful.
Rakudo 会在决定要运行一个 MAIN
子程序时调用一个名为 RUN-MAIN
的子程序.你可以从你的模块中实际导出一个RUN-MAIN
,并设置动态变量,然后调用原来的RUN-MAIN
:
Rakudo will call a subroutine called RUN-MAIN
when it decides there is a MAIN
sub to be run. You can actually export a RUN-MAIN
from your module, and set up the dynamic variable, and then call the original RUN-MAIN
:
sub RUN-MAIN(|c) is export {
my %*SUB-MAIN-OPTS = :named-anywhere;
CORE::<&RUN-MAIN>(|c)
}
有关 RUN-MAIN
的更多信息,请参阅:https://docs.raku.org/language/create-cli#index-entry-RUN-MAIN
For more information about RUN-MAIN
, see: https://docs.raku.org/language/create-cli#index-entry-RUN-MAIN
这篇关于多个 MAIN 签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!