Debian Codegraph @

README.md

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