So recently I ran into a problem with Golang flags and was curious to know if this is as intended, if its a problem or if I'm being a blithering idiot and there is a very simple answer to this.
So using the following code:
func main() {
test := flag.String("-test", "", "test var")
flag.Parse()
if *test != "" {
fmt.Println(*test)
}
}
And then run it using the following command ./main -test 1
You get the following autogenerated error:
flag provided but not defined: -test
Usage of ./main:
--test string
test var
The same happens if you then use ./main --test 1
, you get the same error. The only way I have found around this is to change the flag definition to be test := flag.String("test", "", "test var")
and then run it with ./main -test 1
.
So my question is why can you not use double hyphens with flags? If you can, where did I go wrong when doing this?
You can use double hyphens and this is documented in the package here:
One or two minus signs may be used; they are equivalent.
Having declared your flags, flag.Parse()
will try to remove one or two hyphens from the flags passed and use the remaining characters of each flag to detect whether one has been declared or not. This is done internally by the flag package in parseOne()
.
Declaring a flag with name -test
will literally map it as is, resulting in flag.Parse()
internally looking for the name test
instead of -test
which is the one you actually declared, resulting in the error you see.
Instead, use only the name of the flag when declaring one:
test := flag.String("test", "", "test var")
and you can use this flag with both one (./main -test 1
) or two hyphens (./main --test 2
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With