score:35

Accepted answer

flag.Parse() is being called before your flag is defined.

You have to make sure that all flag definitions happen before calling flag.Parse(), usually by defining all flags inside init() functions.

Similar question

score:1

If you get this, when running command via docker-compose then you do incorrect quoting. Eg.

services:
  app:
    ...
    image: kumina/openvpn-exporter:latest
    command: [  
      "--openvpn.status_paths", "/etc/openvpn_exporter/openvpn-status.log",
      "--openvpn.status_paths /etc/openvpn_exporter/openvpn-status.log",
    ]

First is correct, second is wrong, because whole line counted as one parameter. You need to split them by passing two separate strings, like in first line.

score:3

do not call flag.Parse() in any init()

score:3

I'm very late to the party; but is this broken (again) on Go 1.19.5?

I hit the same errors reported on this thread and the same solution reported above (https://github.com/golang/go/issues/31859#issuecomment-489889428) fixes it.

I see a call to flags.Parse() was added back in go_test.go in v1.18

https://go.googlesource.com/go/+/f7248f05946c1804b5519d0b3eb0db054dc9c5d6%5E%21/src/cmd/go/go_test.go

I am only just learning Go so it'd be nice to have some verification from people more skilled before I report this elsewhere.

score:15

If you've migrated to golang 13, it changed the order of the test initializer, so it could lead to something like

flag provided but not defined: -test.timeout

as a possible workaround, you can use

var _ = func() bool {
    testing.Init()
    return true
}()

that would call test initialization before the application one. More info can be found on the original thread:

https://github.com/golang/go/issues/31859#issuecomment-489889428


Related Query

More Query from same tag