简体   繁体   English

GO 眼镜蛇:StringArray 标志中的空格分隔值

[英]GO cobra: space separated values in StringArray flags

In GO's Cobra, lib for making CLIs, there are two input flags accepting multiple values being passed.在 GO 的 Cobra 中,用于制作 CLI 的库中,有两个输入标志接受传递的多个值。 One of the options is StringArray , when used as follows:其中一个选项是StringArray ,使用时如下:

--flag=value1 --flag=value2

it yields an array ["value1", "value2"] .它产生一个数组["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"]它应该产生的数组是["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? cobra 中有没有一种方法可以解析整个字符串直到下一个标志并将其包含在 StringArray 值中,如上所示?

There isn't a built-in way in cobra to do this, as there will be ambiguity. cobra 中没有内置的方法来执行此操作,因为会有歧义。 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 .例如,在还有一个名为valueB1valueB2的子命令的情况下,不清楚这些是应该作为子命令执行还是解释为--flag的附加参数。

The standard way to support an input like this is to expect the the input values are quoted, and cobra supports that.支持这样的输入的标准方法是期望输入值被引用,cobra 支持这一点。 Eg:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM