package main
import (
\"flag\"
\"fmt\"
)
func main() {
passArguments()
}
func passArguments() string {
username := flag.String(\"user\", \"root\",
Per my comment, the very first value in os.Args
is a (path to) executable itself, so os.Args = []string{"cmd", "-user=bla"}
should fix your issue. You can take a look at flag test from the standard package where they're doing something similar.
Also, as os.Args
is a "global variable", it might be a good idea to keep the state from before the test and restore it after. Similarly to the linked test:
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
This might be useful where other tests are, for example, examining the real arguments passed when evoking go test
.