GO cobra: StringArray标志中的空格分隔值

huangapple go评论63阅读模式
英文:

GO cobra: space separated values in StringArray flags

问题

在GO的Cobra中,用于创建命令行界面的库,有两个接受多个值的输入标志。其中一个选项是StringArray,用法如下:

--flag=value1 --flag=value2

它会生成一个数组["value1", "value2"]
我正在为一个工具开发一个替代品,该工具期望接收稍微复杂一些的输入:

--flag=valueA1 valueB1 --flag=valueA2 valueB2

它应该生成一个数组["valueA1 valueB1", "valueA2 valueB2"]

在Cobra中是否有一种方法可以解析整个字符串直到下一个标志,并将其包含在StringArray值中,就像上面的例子一样?

英文:

In GO's Cobra, lib for making CLIs, there are two input flags accepting multiple values being passed. One of the options is StringArray, when used as follows:

--flag=value1 --flag=value2

it yields an array ["value1", "value2"].
I am working on a drop-in replacement for a tool that expects somewhat more complex input:

--flag=valueA1 valueB1 --flag=valueA2 valueB2

the array it should yield would be ["valueA1 valueB1", "valueA2 valueB2"]

is there a way in cobra to parse the entire string until the next flag and include it in StringArray value like above?

答案1

得分: 3

在 cobra 中没有内置的方法来做到这一点,因为会存在歧义。例如,在存在名为 valueB1valueB2 的子命令的情况下,不清楚这些命令是应该作为子命令执行还是解释为 --flag 的附加参数。

支持这种输入的标准方式是期望输入值被引号引起来,而 cobra 支持这一点。例如:

--flag="valueA1 valueB1" --flag="valueA2 valueB2"
英文:

There isn't a built-in way in cobra to do this, as there will be ambiguity. For example, in the case when there is also a sub-command named valueB1 or valueB2, it's not clear whether those should be executed as subcommands or interpreted as additional argument to --flag.

The standard way to support an input like this is to expect the the input values are quoted, and cobra supports that. E.g.:

--flag="valueA1 valueB1" --flag="valueA2 valueB2"

huangapple
  • 本文由 发表于 2023年1月30日 01:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75277125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定