1go-flags: a go library for parsing command line arguments 2========================================================= 3 4[![GoDoc](https://godoc.org/github.com/jessevdk/go-flags?status.png)](https://godoc.org/github.com/jessevdk/go-flags) [![Build Status](https://travis-ci.org/jessevdk/go-flags.svg?branch=master)](https://travis-ci.org/jessevdk/go-flags) [![Coverage Status](https://img.shields.io/coveralls/jessevdk/go-flags.svg)](https://coveralls.io/r/jessevdk/go-flags?branch=master) 5 6This library provides similar functionality to the builtin flag library of 7go, but provides much more functionality and nicer formatting. From the 8documentation: 9 10Package flags provides an extensive command line option parser. 11The flags package is similar in functionality to the go builtin flag package 12but provides more options and uses reflection to provide a convenient and 13succinct way of specifying command line options. 14 15Supported features: 16* Options with short names (-v) 17* Options with long names (--verbose) 18* Options with and without arguments (bool v.s. other type) 19* Options with optional arguments and default values 20* Multiple option groups each containing a set of options 21* Generate and print well-formatted help message 22* Passing remaining command line arguments after -- (optional) 23* Ignoring unknown command line options (optional) 24* Supports -I/usr/include -I=/usr/include -I /usr/include option argument specification 25* Supports multiple short options -aux 26* Supports all primitive go types (string, int{8..64}, uint{8..64}, float) 27* Supports same option multiple times (can store in slice or last option counts) 28* Supports maps 29* Supports function callbacks 30* Supports namespaces for (nested) option groups 31 32The flags package uses structs, reflection and struct field tags 33to allow users to specify command line options. This results in very simple 34and concise specification of your application options. For example: 35 36```go 37type Options struct { 38 Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"` 39} 40``` 41 42This specifies one option with a short name -v and a long name --verbose. 43When either -v or --verbose is found on the command line, a 'true' value 44will be appended to the Verbose field. e.g. when specifying -vvv, the 45resulting value of Verbose will be {[true, true, true]}. 46 47Example: 48-------- 49```go 50var opts struct { 51 // Slice of bool will append 'true' each time the option 52 // is encountered (can be set multiple times, like -vvv) 53 Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"` 54 55 // Example of automatic marshalling to desired type (uint) 56 Offset uint `long:"offset" description:"Offset"` 57 58 // Example of a callback, called each time the option is found. 59 Call func(string) `short:"c" description:"Call phone number"` 60 61 // Example of a required flag 62 Name string `short:"n" long:"name" description:"A name" required:"true"` 63 64 // Example of a value name 65 File string `short:"f" long:"file" description:"A file" value-name:"FILE"` 66 67 // Example of a pointer 68 Ptr *int `short:"p" description:"A pointer to an integer"` 69 70 // Example of a slice of strings 71 StringSlice []string `short:"s" description:"A slice of strings"` 72 73 // Example of a slice of pointers 74 PtrSlice []*string `long:"ptrslice" description:"A slice of pointers to string"` 75 76 // Example of a map 77 IntMap map[string]int `long:"intmap" description:"A map from string to int"` 78} 79 80// Callback which will invoke callto:<argument> to call a number. 81// Note that this works just on OS X (and probably only with 82// Skype) but it shows the idea. 83opts.Call = func(num string) { 84 cmd := exec.Command("open", "callto:"+num) 85 cmd.Start() 86 cmd.Process.Release() 87} 88 89// Make some fake arguments to parse. 90args := []string{ 91 "-vv", 92 "--offset=5", 93 "-n", "Me", 94 "-p", "3", 95 "-s", "hello", 96 "-s", "world", 97 "--ptrslice", "hello", 98 "--ptrslice", "world", 99 "--intmap", "a:1", 100 "--intmap", "b:5", 101 "arg1", 102 "arg2", 103 "arg3", 104} 105 106// Parse flags from `args'. Note that here we use flags.ParseArgs for 107// the sake of making a working example. Normally, you would simply use 108// flags.Parse(&opts) which uses os.Args 109args, err := flags.ParseArgs(&opts, args) 110 111if err != nil { 112 panic(err) 113} 114 115fmt.Printf("Verbosity: %v\n", opts.Verbose) 116fmt.Printf("Offset: %d\n", opts.Offset) 117fmt.Printf("Name: %s\n", opts.Name) 118fmt.Printf("Ptr: %d\n", *opts.Ptr) 119fmt.Printf("StringSlice: %v\n", opts.StringSlice) 120fmt.Printf("PtrSlice: [%v %v]\n", *opts.PtrSlice[0], *opts.PtrSlice[1]) 121fmt.Printf("IntMap: [a:%v b:%v]\n", opts.IntMap["a"], opts.IntMap["b"]) 122fmt.Printf("Remaining args: %s\n", strings.Join(args, " ")) 123 124// Output: Verbosity: [true true] 125// Offset: 5 126// Name: Me 127// Ptr: 3 128// StringSlice: [hello world] 129// PtrSlice: [hello world] 130// IntMap: [a:1 b:5] 131// Remaining args: arg1 arg2 arg3 132``` 133 134More information can be found in the godocs: <http://godoc.org/github.com/jessevdk/go-flags> 135