如何防止负整数被视为 shorthand 标志

How to prevent negative integer treated as shorthand flag

我需要解析包含负整数的命令行参数,spf13/cobra 是选择的库:

go run main.go write -d 1 -a mock -e int 0 2 -1 

不幸的是,cobra 认为 -1 是一个 shorthand 标志,当然没有定义:

Error: unknown shorthand flag: '1' in -1

我在 -1 周围尝试了单引号和双引号,结果相同。我怎样才能让 Cobra 将负整数作为参数而不是标志?

它按预期工作:无法区分参数和标志。

调用者有责任使用 --:

使其明确
go run main.go write -d 1 -a mock -e int -- 0 2 -1 

-- 表示 "whatever comes after these dashes are arguments"

参考文献: