Consider, my application uses a third party library. I want my application to accept only the flags defined in my application and not the flags defined in imported packages.
package main
import (
"flag"
"fmt"
log "github.com/golang/glog"
)
var myFlag int
func init() {
flag.IntVar(&myFlag, "my_flag", 0, "need only my flags")
confDir := "/Users/foo/test/logs" //assume this is read from configuration file
flag.Set("log_dir", confDir)
flag.Parse()
}
func main() {
flag.Parse()
log.Errorln("flag", myFlag)
log.V(0).Infoln("flag", myFlag)
fmt.Println("test", myFlag)
}
In above code sample, log package has many flags. After compiling when I execute following command, all flags including "my_flag" and flags from log package are displayed. But, I want to set the values for log package flags from my code with values obtained from configuration file.
-alsologtostderr
log to standard error as well as files
-log_backtrace_at value
when logging hits line file:N, emit a stack trace
-log_dir string
If non-empty, write log files in this directory
-logtostderr
log to standard error instead of files
-my_flag int
need only my flags
-stderrthreshold value
logs at or above this threshold go to stderr
-v value
log level for V logs
-vmodule value
comma-separated list of pattern=N settings for file-filtered logging
How to restrict my application executable from accepting other flags?