如何将 space 传递给 Cobra CLI 字符串切片标志?

How to pass space to Cobra CLI string slice flag?

我正在使用 Pflag libraray 中的 StringSliceP 接受字符串列表作为 CLI 参数。

我正在从 Windows 命令提示符调用 Go 应用程序。

我希望列表的某些字符串包含 (") 双引号字符,但我无法做到这一点。

转义引号不起作用:

goapp.exe --string-slice-list "a\"b",c,d,e

预期结果:[]string{"a\"b", "c", "d", "e"}

实际结果:Error: invalid argument "a\"\b,c,d,e" for "--string-slice-list" flag: parse error on line 1, column 1: bare " in non-quoted-field

双引号不起作用:

goapp.exe --string-slice-list "a""b",c,d,e

预期结果:[]string{"a\"b", "c", "d", "e"}

实际结果:Error: invalid argument "a\"b,c,d,e" for "--string-slice-list" flag: parse error on line 1, column 1: bare " in non-quoted-field

以下是如何在 Windows 命令提示符下执行此操作:

goapp.exe --string-slice-list \"a\"\"b\",c,d,e

产量 [a"b c d e]

goapp.exe --string-slice-list \"a\\"\"b\",c,d,e

[a\"b c d e](我不确定你到底想要哪一个)。

正如已经指出的那样,原因是 Pflag library makes use of the Go standard library encoding/csv supporting the format described in RFC 4180。如果我们参考第 5、6 和 7 段中的第 2 节:

If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.