如何从命令行提供非稀疏数组或命名数组? [英] How to provide a non-slurpy array or named array from the command line?

查看:104
本文介绍了如何从命令行提供非稀疏数组或命名数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:Raku(perl6)很棒。 Cro也是。恋爱只花了一个周末。但是现在我偶然发现了一些必须非常简单的东西。

First of all: raku (perl6) is amazing. And so is Cro. It only took a week-end to fall in love. However now I stumble over something that must be extremely simple.

如果我在多重调度MAIN中使用了slurpy参数,则它会被识别并完美运行:

If I use a slurpy parameter in a multiple dispatch MAIN this is recognized and works perfectly:

multi MAIN( 'config', 'add', *@hostnames ) {

但是,如果我将其设置为非糊状数组,则该数组要么无法识别,要么不知道如何从命令行提供它:

However if I make this a non-slurpy array, this is either not recognized or I don't know how to provide it from the command line:

multi MAIN( 'config', 'add', @hostnames ) {

我希望这些调用之一可以起作用:

I would expect one of these invocations to work:

$ cli.raku config add www.example.com example.com
$ cli.raku config add www.example.com,example.com
$ cli.raku config add www.example.com, example.com

Cro CLI ,但是没有在文档

我也尝试过使用数组作为命名参数:

I also tried this with an array as named parameter:

my %*SUB-MAIN-OPTS = :named-anywhere;
multi MAIN( 'config', 'add', :@hostnames) {

给出 raku文档中的示例工作:

$ cli.raku config add --hostnames=www.example.com example.com

但不是,也没有逗号或空格分隔的变体。在所有情况下,我都会获得使用情况信息。

But it does not, nor variants with comma or space comma separation. In all cases I get the usage information.

推荐答案

Raku内置的arg解析与标准的shell功能/约定相对应。正如JJ所指出的,单个阵列没有外壳功能/约定。我想这就是为什么普通 @foo (和%bar )未定义为匹配任何已构建对象的原因在CLI解析功能中。

The arg parsing that is built into Raku corresponds to standard shell features/conventions. As JJ notes, there is no shell feature/convention for individual arrays. I presume this is why plain @foo (and %bar) is not defined to match anything as part of the built in CLI parsing features.

您的示例可能会很草,您还没有说为什么不这样做?

Your example would be covered by a slurpy, and you haven't said why you don't want to use a slurpy.

一个猜测是,这是因为一个混搭会允许零参数。这是解决问题的惯用方式:

One guess is that it's because a slurpy would allow zero arguments. Here's an idiomatic way to fix that:

multi MAIN( 'config', 'add', *@hostnames where +*) {

您可以将 + * 读为一个或多个。

You can read the +* as "one or more".

实际上发生的是我写了一个 where 子句。除了任何其他约束(例如类型)之外,这是对变量或参数施加的约束。 其中子句是任意条件,其值为 True False 。将要绑定到变量/参数的值(如果它通过约束条件)对于该条件隐式为 it。

What's actually going on there is that I've written a where clause. This is a constraint that's imposed on a variable or parameter in addition to any other constraint such as a type. A where clause is an arbitrary condition that evaluates as True or False. The value that is about to be bound to the variable/parameter (if it passes the constraint condition) is implicitly "it" for the condition.

只要表达式包含一个或多个运算符与一个或多个 * s作为操作数组合,Raku将表达式转换为一个函数,其中 * 是该函数的参数。

Whenever an expression contains one or more operator(s) combined with one or more *s as operand(s), Raku converts the expression into a function, where the *(s) are parameters of that function.

所以 + * 很小一个参数函数,仅将前缀 + 应用于其一个参数,也称为 it。

So +* is a tiny little one parameter function that just applies a prefix + to its one argument aka "it".

当您应用前缀时 + 到数组,它返回该数组中元素的 Int 个计数。从条件表达式返回的值被评估为 Bool - True False 。如果它是 0 (即未传递任何参数),则约束条件返回 False ,因此主要签名无法绑定,并且显示使用情况消息。

When you apply prefix + to an array, it returns the Int count of elements in that array. The value returned from the condition expression is evaluated as a Bool -- True or False. If it's 0 (i.e. no arguments were passed), the constraint condition returns False so the MAIN signature fails to bind and the usage message gets displayed.

如果不是,也许是因为最后每个命令行只能使用一个数组。

If that's not it, perhaps it's because you can use only one array slurpy per command line, at the end.

或者只是好奇心。

命名数组的工作方式如下:

A named array works like this:

sub MAIN ( :@n ) {}

my shell prompt> cli-prog.raku -n=www.example.com -n=example.com






[raku] getopt

您可以控制CLI解析以获得所需的结果:

You can take over control of CLI parsing to get whatever result you want:

SuperMAIN ,它是内置的 MAIN 功能的严格超集

SuperMAIN, a strict superset of the built in MAIN functionality.

我在生产环境中构建命令行应用程序的经验(视频幻灯片

"My experience building out a command line application in production" (video, slides)

这篇关于如何从命令行提供非稀疏数组或命名数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆