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
package main import ( "fmt" "os" "github.com/spf13/viper" ) type Config struct { MacPass string `mapstructure:"macos"` LinuxPass string `mapstructure:"linux"` WindowsPass string `mapstructure:"windows"` PostHost string `mapstructure:"postgres"` MySQLHost string `mapstructure:"mysql"` MongoHost string `mapstructure:"mongodb"` } func main() { var configFile = ".config.json" if len(os.Args) == 1 { fmt.Println("Using default config file", configFile) } else { configFile = os.Args[1] } viper.SetConfigType("json") viper.SetConfigFile(configFile) fmt.Printf("Using config file %s\n", viper.ConfigFileUsed()) viper.ReadInConfig() if viper.IsSet("macos") { fmt.Println("macos:", viper.Get("macos")) } else { fmt.Println("macos not set!") } if viper.IsSet("active") { value := viper.GetBool("active") if value { postgres := viper.Get("postgres") mysql := viper.Get("mysql") mongo := viper.Get("mongodb") fmt.Println("P:", postgres, "My:", mysql, "Mo:", mongo) } } else { fmt.Println("active is not set!") } if !viper.IsSet("DoesNotExist") { fmt.Println("DoesNotExist is not set!") } var config Config err := viper.Unmarshal(&config) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Printf("%#v\n", config) }
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go Using default config file .config.json Using config file .config.json macos not set! active is not set! DoesNotExist is not set! main.Config{MacPass:"", LinuxPass:"", WindowsPass:"", PostHost:"", MySQLHost:"", MongoHost:""}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-06-12 Ubuntu - Nvidia Driver