走旗
v1.6.1
該庫提供了與 go 內建標誌庫類似的功能,但提供了更多的功能和更好的格式。從文件中:
套件 flags 提供了一個廣泛的命令列選項解析器。 flags 套件的功能與 go 內建 flag 套件類似,但提供了更多選項,並使用反射來提供一種方便、簡潔的方式來指定命令列選項。
支援的功能:
flags 套件使用結構體、反射和結構體欄位標籤來允許使用者指定命令列選項。這使得您的應用程式選項的規範非常簡單和簡潔。例如:
type Options struct {
Verbose [] bool `short:"v" long:"verbose" description:"Show verbose debug information"`
}
這指定了一個帶有短名稱 -v 和長名稱 --verbose 的選項。當在命令列上找到 -v 或 --verbose 時,「true」值將附加到 Verbose 欄位。例如,當指定 -vvv 時,Verbose 的結果值為 {[true, true, true]}。
var opts struct {
// Slice of bool will append 'true' each time the option
// is encountered (can be set multiple times, like -vvv)
Verbose [] bool `short:"v" long:"verbose" description:"Show verbose debug information"`
// Example of automatic marshalling to desired type (uint)
Offset uint `long:"offset" description:"Offset"`
// Example of a callback, called each time the option is found.
Call func ( string ) `short:"c" description:"Call phone number"`
// Example of a required flag
Name string `short:"n" long:"name" description:"A name" required:"true"`
// Example of a flag restricted to a pre-defined set of strings
Animal string `long:"animal" choice:"cat" choice:"dog"`
// Example of a value name
File string `short:"f" long:"file" description:"A file" value-name:"FILE"`
// Example of a pointer
Ptr * int `short:"p" description:"A pointer to an integer"`
// Example of a slice of strings
StringSlice [] string `short:"s" description:"A slice of strings"`
// Example of a slice of pointers
PtrSlice [] * string `long:"ptrslice" description:"A slice of pointers to string"`
// Example of a map
IntMap map [ string ] int `long:"intmap" description:"A map from string to int"`
// Example of env variable
Thresholds [] int `long:"thresholds" default:"1" default:"2" env:"THRESHOLD_VALUES" env-delim:","`
}
// Callback which will invoke callto: to call a number.
// Note that this works just on OS X (and probably only with
// Skype) but it shows the idea.
opts . Call = func ( num string ) {
cmd := exec . Command ( "open" , "callto:" + num )
cmd . Start ()
cmd . Process . Release ()
}
// Make some fake arguments to parse.
args := [] string {
"-vv" ,
"--offset=5" ,
"-n" , "Me" ,
"--animal" , "dog" , // anything other than "cat" or "dog" will raise an error
"-p" , "3" ,
"-s" , "hello" ,
"-s" , "world" ,
"--ptrslice" , "hello" ,
"--ptrslice" , "world" ,
"--intmap" , "a:1" ,
"--intmap" , "b:5" ,
"arg1" ,
"arg2" ,
"arg3" ,
}
// Parse flags from `args'. Note that here we use flags.ParseArgs for
// the sake of making a working example. Normally, you would simply use
// flags.Parse(&opts) which uses os.Args
args , err := flags . ParseArgs ( & opts , args )
if err != nil {
panic ( err )
}
fmt . Printf ( "Verbosity: %v n " , opts . Verbose )
fmt . Printf ( "Offset: %d n " , opts . Offset )
fmt . Printf ( "Name: %s n " , opts . Name )
fmt . Printf ( "Animal: %s n " , opts . Animal )
fmt . Printf ( "Ptr: %d n " , * opts . Ptr )
fmt . Printf ( "StringSlice: %v n " , opts . StringSlice )
fmt . Printf ( "PtrSlice: [%v %v] n " , * opts . PtrSlice [ 0 ], * opts . PtrSlice [ 1 ])
fmt . Printf ( "IntMap: [a:%v b:%v] n " , opts . IntMap [ "a" ], opts . IntMap [ "b" ])
fmt . Printf ( "Remaining args: %s n " , strings . Join ( args , " " ))
// Output: Verbosity: [true true]
// Offset: 5
// Name: Me
// Ptr: 3
// StringSlice: [hello world]
// PtrSlice: [hello world]
// IntMap: [a:1 b:5]
// Remaining args: arg1 arg2 arg3
更多資訊可以在 godocs 中找到:http://godoc.org/github.com/jessevdk/go-flags