Debian Codegraph @

flags.go

raw symbols SCIP
1 // Copyright 2012 Jesse van den Kieboom. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package flags provides an extensive command line option parser.
6 // The flags package is similar in functionality to the go builtin flag package
7 // but provides more options and uses reflection to provide a convenient and
8 // succinct way of specifying command line options.
9 //
10 // Supported features:
11 // Options with short names (-v)
12 // Options with long names (--verbose)
13 // Options with and without arguments (bool v.s. other type)
14 // Options with optional arguments and default values
15 // Multiple option groups each containing a set of options
16 // Generate and print well-formatted help message
17 // Passing remaining command line arguments after -- (optional)
18 // Ignoring unknown command line options (optional)
19 // Supports -I/usr/include -I=/usr/include -I /usr/include option argument specification
20 // Supports multiple short options -aux
21 // Supports all primitive go types (string, int{8..64}, uint{8..64}, float)
22 // Supports same option multiple times (can store in slice or last option counts)
23 // Supports maps
24 // Supports function callbacks
25 //
26 // Additional features specific to Windows:
27 // Options with short names (/v)
28 // Options with long names (/verbose)
29 // Windows-style options with arguments use a colon as the delimiter
30 // Modify generated help message with Windows-style / options
31 //
32 // The flags package uses structs, reflection and struct field tags
33 // to allow users to specify command line options. This results in very simple
34 // and consise specification of your application options. For example:
35 //
36 // type Options struct {
37 // Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
38 // }
39 //
40 // This specifies one option with a short name -v and a long name --verbose.
41 // When either -v or --verbose is found on the command line, a 'true' value
42 // will be appended to the Verbose field. e.g. when specifying -vvv, the
43 // resulting value of Verbose will be {[true, true, true]}.
44 //
45 // Slice options work exactly the same as primitive type options, except that
46 // whenever the option is encountered, a value is appended to the slice.
47 //
48 // Map options from string to primitive type are also supported. On the command
49 // line, you specify the value for such an option as key:value. For example
50 //
51 // type Options struct {
52 // AuthorInfo string[string] `short:"a"`
53 // }
54 //
55 // Then, the AuthorInfo map can be filled with something like
56 // -a name:Jesse -a "surname:van den Kieboom".
57 //
58 // Finally, for full control over the conversion between command line argument
59 // values and options, user defined types can choose to implement the Marshaler
60 // and Unmarshaler interfaces.
61 //
62 // Available field tags:
63 // short: the short name of the option (single character)
64 // long: the long name of the option
65 // description: the description of the option (optional)
66 // optional: whether an argument of the option is optional (optional)
67 // optional-value: the value of an optional option when the option occurs
68 // without an argument. This tag can be specified multiple
69 // times in the case of maps or slices (optional)
70 // default: the default value of an option. This tag can be specified
71 // multiple times in the case of slices or maps (optional).
72 // default-mask: when specified, this value will be displayed in the help
73 // instead of the actual default value. This is useful
74 // mostly for hiding otherwise sensitive information from
75 // showing up in the help. If default-mask takes the special
76 // value "-", then no default value will be shown at all
77 // (optional)
78 // required: whether an option is required to appear on the command
79 // line. If a required option is not present, the parser
80 // will return ErrRequired.
81 // base: a base (radix) used to convert strings to integer values,
82 // the default base is 10 (i.e. decimal) (optional)
83 // value-name: the name of the argument value (to be shown in the help,
84 // (optional)
85 // group: when specified on a struct field, makes the struct field
86 // a separate group with the given name (optional).
87 // command: when specified on a struct field, makes the struct field
88 // a (sub)command with the given name (optional).
89 // name: the display name of the command. This name is the name
90 // shown in the builtin generated help and can be used
91 // to provide a more informative title of a command. If not
92 // specified this defaults to the name given in the command
93 // command tag (optional).
94 //
95 // Either short: or long: must be specified to make the field eligible as an
96 // option.
97 //
98 //
99 // Option groups:
100 //
101 // Option groups are a simple way to semantically separate your options. The
102 // only real difference is in how your options will appear in the builtin
103 // generated help. All options in a particular group are shown together in the
104 // help under the name of the group.
105 //
106 // There are currently three ways to specify option groups.
107 //
108 // 1. Use NewNamedParser specifying the various option groups.
109 // 2. Use AddGroup to add a group to an existing parser.
110 // 3. Add a struct field to the toplevel options annotated with the
111 // group:"group-name" tag.
112 //
113 //
114 //
115 // Commands:
116 //
117 // The flags package also has basic support for commands. Commands are often
118 // used in monolithic applications that support various commands or actions.
119 // Take git for example, all of the add, commit, checkout, etc. are called
120 // commands. Using commands you can easily separate multiple functions of your
121 // application.
122 //
123 // There are currently two ways to specifiy a command.
124 //
125 // 1. Use AddCommand on an existing parser.
126 // 2. Add a struct field to your options struct annotated with the
127 // command:"command-name" tag.
128 //
129 // The most common, idiomatic way to implement commands is to define a global
130 // parser instance and implement each command in a separate file. These
131 // command files should define a go init function which calls AddCommand on
132 // the global parser.
133 //
134 // When parsing ends and there is an active command and that command implements
135 // the Command interface, then its Execute method will be run providing the
136 // remaining arguments.
137 //
138 // Command structs can have options which become valid to parse after the
139 // command has been specified on the command line. It is currently not valid
140 // to specify options from the parent level of the command after the command
141 // name has occurred. Thus, given a toplevel option "-v" and a command "add":
142 //
143 // Valid: ./app -v add
144 // Invalid: ./app add -v
145 //
146 package flags