Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with golang flags

Tags:

go

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?

like image 781
John Avatar asked Jan 04 '23 03:01

John


1 Answers

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).

like image 72
0xbadbeef Avatar answered Jan 13 '23 10:01

0xbadbeef