I have a main.go
and mypkg/...go
. I use go build -o main main.go
or go install <pkg that has main.go>
and which has some flags I require. But I also see the test flags. Why is this happening? What am I missing?
Usage of ./main:
-docker string
Docker API Path, defaults to local (default "unix:///var/run/docker.sock")
-httptest.serve string
if non-empty, httptest.NewServer serves on this address and blocks
-port int
The default port to listen (default 8000)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
dockerPath and port are my flags, but as you can see the others are not my flags.
Most likely you're using the default flag set (flag.FlagSet
) of the flag
package. And note that you may not be the only one using it. If you import other packages, they might also register flags, which will be processed just like your own flags (flags you registered).
See this simple example:
import (
"flag"
_ "testing"
)
func main() {
flag.Int("port", 80, "port to use")
flag.Parse()
}
This app registers a port
flag, and nothing else. But it also imports the testing
package which registers a lot of flags.
Running it with the -h
command line argument, the output is:
-port int
port to use (default 80)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
-test.count n
run tests and benchmarks n times (default 1)
-test.coverprofile string
write a coverage profile to the named file after execution
-test.cpu string
comma-separated list of number of CPUs to use for each test
-test.cpuprofile string
write a cpu profile to the named file during execution
-test.memprofile string
write a memory profile to the named file after execution
-test.memprofilerate int
if >=0, sets runtime.MemProfileRate
-test.outputdir string
directory in which to write profiles
-test.parallel int
maximum test parallelism (default 4)
-test.run string
regular expression to select tests and examples to run
-test.short
run smaller test suite to save time
-test.timeout duration
if positive, sets an aggregate time limit for all tests
-test.trace string
write an execution trace to the named file after execution
-test.v
verbose: print additional output
exit status 2
If you don't want your flags to be mixed with flags of other packages, create and use your own flag.FlagSet
by calling flag.NewFlagSet()
, but then of course you have to use its methods instead of the top-level functions of the flag
package.
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