pflag is a drop-in replacement of Go's native flag package. If you import pflag under the name "flag" then all code should continue to function with no changes.
import flag "github.com/spf13/pflag"
There is one exception to this: if you directly instantiate the Flag struct there is one more field "Shorthand" that you will need to set. Most code never instantiates this struct directly, and instead uses functions such as String(), BoolVar(), and Var(), and is therefore unaffected.
package main import ( "fmt" "github.com/spf13/pflag" "github.com/spf13/viper" ) func aliasNormalizeFunc(f *pflag.FlagSet, n string) pflag.NormalizedName { switch n { case "pass", "ps": n = "password" } return pflag.NormalizedName(n) } func main() { pflag.StringP("name", "n", "Frank", "Name parameter") pflag.StringP("password", "p", "hardToGuess", "Password") pflag.CommandLine.SetNormalizeFunc(aliasNormalizeFunc) pflag.Parse() viper.BindPFlags(pflag.CommandLine) name := viper.GetString("name") password := viper.GetString("password") fmt.Println(name, password) // Reading an environment variable viper.BindEnv("GOMAXPROCS") val := viper.Get("GOMAXPROCS") if val != nil { fmt.Println("GOMAXPROCS:", val) } else { // Setting an environment variable viper.Set("GOMAXPROCS", 8) val = viper.Get("GOMAXPROCS") fmt.Println("GOMAXPROCS:", val) } }
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go --help Usage of /tmp/go-build3847677257/b001/exe/main: -n, --name string Name parameter (default "Frank") -p, --password string Password (default "hardToGuess") pflag: help requested exit status 2 zzh@ZZHPC:/zdata/Github/ztest$ go run main.go -n ZhangZhihui -p ZZZ ZhangZhihui ZZZ GOMAXPROCS: 8 zzh@ZZHPC:/zdata/Github/ztest$ go run main.go Frank hardToGuess GOMAXPROCS: 8
标签:password,pflag,GOMAXPROCS,viper,go,Go,main From: https://www.cnblogs.com/zhangzhihui/p/18244285