andig
andig

Reputation: 13878

How to prevent negative integer treated as shorthand flag

I need to parse command line args containing negative ints, spf13/cobra is the library of choice:

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

Unfortunately cobra thinks that -1 is a shorthand flag which is of course not defined:

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

I've tried single and double quotest around -1 with same result. How can I have cobra leave negative ints as args instead of flags?

Upvotes: 2

Views: 949

Answers (1)

zerkms
zerkms

Reputation: 254924

It works as expected: it's impossible to distinguish between arguments and flags.

It's the caller responsibility to make it unambiguous using --:

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

-- means "whatever comes after these dashes are arguments"

References:

Upvotes: 4

Related Questions