blob: a6acf1be1f3f5a2b2ec26e7434ae67bdf1523e47 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301// 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/*
6Package flags provides an extensive command line option parser.
7The flags package is similar in functionality to the go built-in flag package
8but provides more options and uses reflection to provide a convenient and
9succinct way of specifying command line options.
10
Abhay Kumarfe505f22025-11-10 14:16:31 +000011# Supported features
Naveen Sampath04696f72022-06-13 15:19:14 +053012
13The following features are supported in go-flags:
14
Abhay Kumarfe505f22025-11-10 14:16:31 +000015 Options with short names (-v)
16 Options with long names (--verbose)
17 Options with and without arguments (bool v.s. other type)
18 Options with optional arguments and default values
19 Option default values from ENVIRONMENT_VARIABLES, including slice and map 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
Naveen Sampath04696f72022-06-13 15:19:14 +053031
32Additional features specific to Windows:
Naveen Sampath04696f72022-06-13 15:19:14 +053033
Abhay Kumarfe505f22025-11-10 14:16:31 +000034 Options with short names (/v)
35 Options with long names (/verbose)
36 Windows-style options with arguments use a colon as the delimiter
37 Modify generated help message with Windows-style / options
38 Windows style options can be disabled at build time using the "forceposix"
39 build tag
Naveen Sampath04696f72022-06-13 15:19:14 +053040
Abhay Kumarfe505f22025-11-10 14:16:31 +000041# Basic usage
Naveen Sampath04696f72022-06-13 15:19:14 +053042
43The flags package uses structs, reflection and struct field tags
44to allow users to specify command line options. This results in very simple
45and concise specification of your application options. For example:
46
Abhay Kumarfe505f22025-11-10 14:16:31 +000047 type Options struct {
48 Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
49 }
Naveen Sampath04696f72022-06-13 15:19:14 +053050
51This specifies one option with a short name -v and a long name --verbose.
52When either -v or --verbose is found on the command line, a 'true' value
53will be appended to the Verbose field. e.g. when specifying -vvv, the
54resulting value of Verbose will be {[true, true, true]}.
55
56Slice options work exactly the same as primitive type options, except that
57whenever the option is encountered, a value is appended to the slice.
58
59Map options from string to primitive type are also supported. On the command
60line, you specify the value for such an option as key:value. For example
61
Abhay Kumarfe505f22025-11-10 14:16:31 +000062 type Options struct {
63 AuthorInfo string[string] `short:"a"`
64 }
Naveen Sampath04696f72022-06-13 15:19:14 +053065
66Then, the AuthorInfo map can be filled with something like
67-a name:Jesse -a "surname:van den Kieboom".
68
69Finally, for full control over the conversion between command line argument
70values and options, user defined types can choose to implement the Marshaler
71and Unmarshaler interfaces.
72
Abhay Kumarfe505f22025-11-10 14:16:31 +000073# Available field tags
Naveen Sampath04696f72022-06-13 15:19:14 +053074
75The following is a list of tags for struct fields supported by go-flags:
76
Abhay Kumarfe505f22025-11-10 14:16:31 +000077 short: the short name of the option (single character)
78 long: the long name of the option
79 required: if non empty, makes the option required to appear on the command
80 line. If a required option is not present, the parser will
81 return ErrRequired (optional)
82 description: the description of the option (optional)
83 long-description: the long description of the option. Currently only
84 displayed in generated man pages (optional)
85 no-flag: if non-empty, this field is ignored as an option (optional)
Naveen Sampath04696f72022-06-13 15:19:14 +053086
Abhay Kumarfe505f22025-11-10 14:16:31 +000087 optional: if non-empty, makes the argument of the option optional. When an
88 argument is optional it can only be specified using
89 --option=argument (optional)
90 optional-value: the value of an optional option when the option occurs
91 without an argument. This tag can be specified multiple
92 times in the case of maps or slices (optional)
93 default: the default value of an option. This tag can be specified
94 multiple times in the case of slices or maps (optional)
95 default-mask: when specified, this value will be displayed in the help
96 instead of the actual default value. This is useful
97 mostly for hiding otherwise sensitive information from
98 showing up in the help. If default-mask takes the special
99 value "-", then no default value will be shown at all
100 (optional)
101 env: the default value of the option is overridden from the
102 specified environment variable, if one has been defined.
103 (optional)
104 env-delim: the 'env' default value from environment is split into
105 multiple values with the given delimiter string, use with
106 slices and maps (optional)
107 value-name: the name of the argument value (to be shown in the help)
108 (optional)
109 choice: limits the values for an option to a set of values.
110 Repeat this tag once for each allowable value.
111 e.g. `long:"animal" choice:"cat" choice:"dog"`
112 hidden: if non-empty, the option is not visible in the help or man page.
Naveen Sampath04696f72022-06-13 15:19:14 +0530113
Abhay Kumarfe505f22025-11-10 14:16:31 +0000114 base: a base (radix) used to convert strings to integer values, the
115 default base is 10 (i.e. decimal) (optional)
Naveen Sampath04696f72022-06-13 15:19:14 +0530116
Abhay Kumarfe505f22025-11-10 14:16:31 +0000117 ini-name: the explicit ini option name (optional)
118 no-ini: if non-empty this field is ignored as an ini option
119 (optional)
Naveen Sampath04696f72022-06-13 15:19:14 +0530120
Abhay Kumarfe505f22025-11-10 14:16:31 +0000121 group: when specified on a struct field, makes the struct
122 field a separate group with the given name (optional)
123 namespace: when specified on a group struct field, the namespace
124 gets prepended to every option's long name and
125 subgroup's namespace of this group, separated by
126 the parser's namespace delimiter (optional)
127 env-namespace: when specified on a group struct field, the env-namespace
128 gets prepended to every option's env key and
129 subgroup's env-namespace of this group, separated by
130 the parser's env-namespace delimiter (optional)
131 command: when specified on a struct field, makes the struct
132 field a (sub)command with the given name (optional)
133 subcommands-optional: when specified on a command struct field, makes
134 any subcommands of that command optional (optional)
135 alias: when specified on a command struct field, adds the
136 specified name as an alias for the command. Can be
137 be specified multiple times to add more than one
138 alias (optional)
139 positional-args: when specified on a field with a struct type,
140 uses the fields of that struct to parse remaining
141 positional command line arguments into (in order
142 of the fields). If a field has a slice type,
143 then all remaining arguments will be added to it.
144 Positional arguments are optional by default,
145 unless the "required" tag is specified together
146 with the "positional-args" tag. The "required" tag
147 can also be set on the individual rest argument
148 fields, to require only the first N positional
149 arguments. If the "required" tag is set on the
150 rest arguments slice, then its value determines
151 the minimum amount of rest arguments that needs to
152 be provided (e.g. `required:"2"`) (optional)
153 positional-arg-name: used on a field in a positional argument struct; name
154 of the positional argument placeholder to be shown in
155 the help (optional)
Naveen Sampath04696f72022-06-13 15:19:14 +0530156
157Either the `short:` tag or the `long:` must be specified to make the field eligible as an
158option.
159
Abhay Kumarfe505f22025-11-10 14:16:31 +0000160# Option groups
Naveen Sampath04696f72022-06-13 15:19:14 +0530161
162Option groups are a simple way to semantically separate your options. All
163options in a particular group are shown together in the help under the name
164of the group. Namespaces can be used to specify option long names more
165precisely and emphasize the options affiliation to their group.
166
167There are currently three ways to specify option groups.
168
Abhay Kumarfe505f22025-11-10 14:16:31 +0000169 1. Use NewNamedParser specifying the various option groups.
170 2. Use AddGroup to add a group to an existing parser.
171 3. Add a struct field to the top-level options annotated with the
172 group:"group-name" tag.
Naveen Sampath04696f72022-06-13 15:19:14 +0530173
Abhay Kumarfe505f22025-11-10 14:16:31 +0000174# Commands
Naveen Sampath04696f72022-06-13 15:19:14 +0530175
176The flags package also has basic support for commands. Commands are often
177used in monolithic applications that support various commands or actions.
178Take git for example, all of the add, commit, checkout, etc. are called
179commands. Using commands you can easily separate multiple functions of your
180application.
181
182There are currently two ways to specify a command.
183
Abhay Kumarfe505f22025-11-10 14:16:31 +0000184 1. Use AddCommand on an existing parser.
185 2. Add a struct field to your options struct annotated with the
186 command:"command-name" tag.
Naveen Sampath04696f72022-06-13 15:19:14 +0530187
188The most common, idiomatic way to implement commands is to define a global
189parser instance and implement each command in a separate file. These
190command files should define a go init function which calls AddCommand on
191the global parser.
192
193When parsing ends and there is an active command and that command implements
194the Commander interface, then its Execute method will be run with the
195remaining command line arguments.
196
197Command structs can have options which become valid to parse after the
198command has been specified on the command line, in addition to the options
199of all the parent commands. I.e. considering a -v flag on the parser and an
200add command, the following are equivalent:
201
Abhay Kumarfe505f22025-11-10 14:16:31 +0000202 ./app -v add
203 ./app add -v
Naveen Sampath04696f72022-06-13 15:19:14 +0530204
205However, if the -v flag is defined on the add command, then the first of
206the two examples above would fail since the -v flag is not defined before
207the add command.
208
Abhay Kumarfe505f22025-11-10 14:16:31 +0000209# Completion
Naveen Sampath04696f72022-06-13 15:19:14 +0530210
211go-flags has builtin support to provide bash completion of flags, commands
212and argument values. To use completion, the binary which uses go-flags
213can be invoked in a special environment to list completion of the current
214command line argument. It should be noted that this `executes` your application,
215and it is up to the user to make sure there are no negative side effects (for
216example from init functions).
217
218Setting the environment variable `GO_FLAGS_COMPLETION=1` enables completion
219by replacing the argument parsing routine with the completion routine which
220outputs completions for the passed arguments. The basic invocation to
221complete a set of arguments is therefore:
222
Abhay Kumarfe505f22025-11-10 14:16:31 +0000223 GO_FLAGS_COMPLETION=1 ./completion-example arg1 arg2 arg3
Naveen Sampath04696f72022-06-13 15:19:14 +0530224
225where `completion-example` is the binary, `arg1` and `arg2` are
226the current arguments, and `arg3` (the last argument) is the argument
227to be completed. If the GO_FLAGS_COMPLETION is set to "verbose", then
228descriptions of possible completion items will also be shown, if there
229are more than 1 completion items.
230
231To use this with bash completion, a simple file can be written which
232calls the binary which supports go-flags completion:
233
Abhay Kumarfe505f22025-11-10 14:16:31 +0000234 _completion_example() {
235 # All arguments except the first one
236 args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
Naveen Sampath04696f72022-06-13 15:19:14 +0530237
Abhay Kumarfe505f22025-11-10 14:16:31 +0000238 # Only split on newlines
239 local IFS=$'\n'
Naveen Sampath04696f72022-06-13 15:19:14 +0530240
Abhay Kumarfe505f22025-11-10 14:16:31 +0000241 # Call completion (note that the first element of COMP_WORDS is
242 # the executable itself)
243 COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
244 return 0
245 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530246
Abhay Kumarfe505f22025-11-10 14:16:31 +0000247 complete -F _completion_example completion-example
Naveen Sampath04696f72022-06-13 15:19:14 +0530248
249Completion requires the parser option PassDoubleDash and is therefore enforced if the environment variable GO_FLAGS_COMPLETION is set.
250
251Customized completion for argument values is supported by implementing
252the flags.Completer interface for the argument value type. An example
253of a type which does so is the flags.Filename type, an alias of string
254allowing simple filename completion. A slice or array argument value
255whose element type implements flags.Completer will also be completed.
256*/
257package flags