jessevdk/go-flags

GitHub: jessevdk/go-flags

一个基于 struct tag 和反射机制的 Go 命令行参数解析库,提供比标准库 flag 包更丰富的选项类型、分组、校验和自动帮助生成能力。

Stars: 2698 | Forks: 325

# go-flags:一个用于解析命令行参数的 go 库 [![GoDoc](https://godoc.org/github.com/jessevdk/go-flags?status.png)](https://godoc.org/github.com/jessevdk/go-flags) 该库提供了与 go 内置的 flag 库类似的功能, 但提供了更多的功能和更好的格式化。摘自 文档: flags 包提供了一个功能强大的命令行选项解析器。 flags 包的功能类似于 go 内置的 flag 包, 但提供了更多的选项,并使用 reflection 提供了一种便捷且 简洁的方式来指定命令行选项。 支持的特性: * 带有短名称的选项 (-v) * 带有长名称的选项 (--verbose) * 带有和不带有参数的选项(bool 类型与其他类型) * 带有可选参数和默认值的选项 * 多个选项组,每个选项组包含一组选项 * 生成并打印格式良好的帮助信息 * 允许传递 -- 之后的剩余命令行参数(可选) * 忽略未知的命令行选项(可选) * 支持 -I/usr/include -I=/usr/include -I /usr/include 选项参数格式 * 支持多个短选项 -aux * 支持所有 go 原始类型 (string, int{8..64}, uint{8..64}, float) * 支持多次使用同一个选项(可存储在 slice 中,或保留最后一个选项的值) * 支持 maps * 支持函数回调 * 支持(嵌套)选项组的 namespace flags 包使用 struct、reflection 和 struct field tag 允许用户指定命令行选项。这使得指定应用程序选项变得非常简单 且精确。例如: ``` 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 中找到:
标签:EVTX分析, Go, Ruby工具, SOC Prime, 参数解析, 命令行解析, 基础库, 开发工具, 日志审计