首页 > 其他分享 >Go - pflag and viper

Go - pflag and viper

时间:2024-06-12 16:59:44浏览次数:30  
标签:password pflag GOMAXPROCS viper go Go main

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

相关文章

  • Dragon Boat Festival
    BeforetheDragonBoatFestival,mygrandmamadedozensofzongzimadeupofreedleaves,polishedglutinousriceandmeat.Mygrandmamakesdeliciouszongzieveryyear.Idon’tknowhowtomakeit.TheDragonBoatFestivalcamesilently.Onthemorning,......
  • go http请求
    funcSend(notificationmodel.Notification,defaultRobotstring)(errerror){markdown,robotURL,err:=transformer.TransformToMarkdown(notification)iferr!=nil{return}data,err:=json.Marshal(markdown)iferr!=nil{......
  • Go版RuoYi
     RuoYi-Go  https://github.com/Kun-GitHub/RuoYi-Go1.关于我个人介绍2.介绍后端用Go写的RuoYi权限管理系统(功能正在持续实现)后端 Gitee地址3.前端RuoYi-Vue3官方前端Vue3版4.Go后端技术栈(持续在对齐项目,在补充)功能框架是否采用备注配置......
  • The dragon Boat Festival
    TheDragonBoatFestival,alsoknownastheDuanwuFestival,isatraditionalChineseholidaycelebratedonthefifthdayofthefifthmonthofthelunarcalendar.ItfallsonJune3rdthisyear.Thisfestivalhasahistoryspanningover2000yearsandis......
  • Dragon Boat Festival
    essenceofthisfestivalAsweallknow,DragonBoatFestivalisatraditionalchinesefestivaltoshowourrespecttoQuYuan.Dragonboatracing,eatingzongzi,prayingforblessings,andwardingoffevilthingsarethecustomthemesoftheDragonBoat......
  • Dragon Boat Festival
    AstheDragonBoatFestivalapproaches,ourcommunityburstswithexcitementandanticipationforthisholiday.Inmyeyes,thisfestivalisrootedinourChinesehistoryandcultureandisfamouswithawell-knownpeoplecalledQuyuan.Inourcommunity,......
  • Zgo - Writing to a file
     packagemainimport("bufio""fmt""io""os")funcmain(){buffer:=[]byte("Datatowrite\n")f1,err:=os.Create("f1.txt")iferr!=nil{fmt.Pri......
  • On the Parameterization and Initialization of Diagonal State Space Models
    目录概符号说明S4D代码GuA.,GuptaA.,GoelK.andReC.Ontheparameterizationandinitializationofdiagonalstatespacemodels.NeurIPS,2022.概Mamba系列第四作:S4D.符号说明\(u(t)\in\mathbb{R}\),输入信号;\(x(t)\in\mathbb{R}^N\),中间状态;\(......
  • Dragon Boat Festival
    BeforetheDragonBoatFestival,mygrandmamadedozensofzongzimadeupofreedleaves,polishedglutinousriceandmeat.Mygrandmamakesdeliciouszongzieveryyear.Idon’tknowhowtomakeit.TheDragonBoatFestivalcamesilently.Onthemorning,......
  • The Dragon Boat festival
    TheDragonBoatFestival,celebratedonthefifthdayofthefifthlunarmonth,isatimeofgreatjoyandexcitementinChina.Itisafestivalrichinhistoryandtradition,honoringthememoryoftheancientpoetQuYuan.Oneofthemostbelovedtradit......