文档:https://godoc.org/github.com/spf13/pflag
Variables
#CommandLine是默认的命令行flag集,用于解析os.Args
#所有通过pflag注册的命令行,都会注册进CommandLine
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
包函数
func Parse() 解析
func Parsed() bool 判断是否有解析
func Lookup(name string) Flag 返回指定命令行标志的标志结构,如果不存在,则返回nil
func BoolVarP(p bool, name, shorthand string, value bool, usage string)
- 将输入值存储到p
- name为参数全称
- shorthand 为参数简称
- value 为默认值
- usage 为用法
func CountVarP(p int, name, shorthand string, usage string)
func DurationVarP(p time.Duration, name, shorthand string, value time.Duration, usage string)
func Float32VarP(p float32, name, shorthand string, value float32, usage string)
func Float64VarP(p float64, name, shorthand string, value float64, usage string)
。。。。。。。。。。其它类型的解析不一一举例
type Flag
{
Name string // name as it appears on command line
Shorthand string // one-letter abbreviated flag
Usage string // help message
Value Value // value as set
DefValue string // default value (as text); for usage message
Changed bool // If the user set the value (or if left to default)
NoOptDefVal string // default value (as text); if the flag is on the command line without any options
Deprecated string // If this flag is deprecated, this string is the new or now thing to use
Hidden bool // used by cobra.Command to allow flags to be hidden from help/usage text
ShorthandDeprecated string // If the shorthand of this flag is deprecated, this string is the new or now thing to use
Annotations map[string][]string // used by cobra.Command bash autocomple code
}
type FlagSet
标志集表示一组定义的标志
type FlagSet struct {
// Usage is the function called when an error occurs while parsing flags.
// The field is a function (not a method) that may be changed to point to
// a custom error handler.
Usage func()
// SortFlags is used to indicate, if user wants to have sorted flags in
// help/usage messages.
SortFlags bool
// ParseErrorsWhitelist is used to configure a whitelist of errors
ParseErrorsWhitelist ParseErrorsWhitelist
// contains filtered or unexported fields
}
func NewFlagSet(name string, errorHandling ErrorHandling) FlagSet 一般使用CommandLine即可
func (f FlagSet) AddFlag(flag Flag)
func (f FlagSet) AddFlagSet(newSet FlagSet)
func (f FlagSet) AddGoFlag(goflag goflag.Flag)
func (f FlagSet) AddGoFlagSet(newSet goflag.FlagSet)
func (f FlagSet) Arg(i int) string 返回第i个参数。Arg(0)是处理标记后的第一个剩余参数(即不包含程序名)
func (f *FlagSet) Args() []string 返回所有参数
func (f FlagSet) Lookup(name string) Flag 返回指定命令行标志的标志结构,如果不存在,则返回nil
func (f FlagSet) BoolVarP(p bool, name, shorthand string, value bool, usage string)
- 将输入值存储到p
- name为参数全称
- shorthand 为参数简称
- value 为默认值
- usage 为用法
func (f FlagSet) CountVarP(p int, name, shorthand string, usage string)
func (f FlagSet) DurationVarP(p time.Duration, name, shorthand string, value time.Duration, usage string)
func (f FlagSet) Float32VarP(p float32, name, shorthand string, value float32, usage string)
func (f FlagSet) Float64VarP(p float64, name, shorthand string, value float64, usage string)
。。。。。。。。。。其它类型的解析不一一举例
例子:
一般使用
var (
config string
casbinModel string
)
StartCmd.PersistentFlags().StringVarP(&config, "config", "c", "config/app.toml", "Start server with provided configuration file")
StartCmd.PersistentFlags().StringVarP(&casbinModel, "casbinmodel", "m", "config/casbin_model.conf", "Start server with provided casbinmodel file")
flag、pflag、viper结合
func main() {
// using standard library "flag" package
flag.Int("flag", 1234, "help message for flagname")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse() // 这里pflag.CommandLine.Parse()是不起作用的
viper.BindPFlags(pflag.CommandLine)
i := viper.GetInt("flag") // retrieve value from viper
fmt.Println(i)
}