annotate libgo/go/flag/flag.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 /*
kono
parents:
diff changeset
6 Package flag implements command-line flag parsing.
kono
parents:
diff changeset
7
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
8 Usage
111
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 Define flags using flag.String(), Bool(), Int(), etc.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
kono
parents:
diff changeset
13 import "flag"
kono
parents:
diff changeset
14 var ip = flag.Int("flagname", 1234, "help message for flagname")
kono
parents:
diff changeset
15 If you like, you can bind the flag to a variable using the Var() functions.
kono
parents:
diff changeset
16 var flagvar int
kono
parents:
diff changeset
17 func init() {
kono
parents:
diff changeset
18 flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
kono
parents:
diff changeset
19 }
kono
parents:
diff changeset
20 Or you can create custom flags that satisfy the Value interface (with
kono
parents:
diff changeset
21 pointer receivers) and couple them to flag parsing by
kono
parents:
diff changeset
22 flag.Var(&flagVal, "name", "help message for flagname")
kono
parents:
diff changeset
23 For such flags, the default value is just the initial value of the variable.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 After all flags are defined, call
kono
parents:
diff changeset
26 flag.Parse()
kono
parents:
diff changeset
27 to parse the command line into the defined flags.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 Flags may then be used directly. If you're using the flags themselves,
kono
parents:
diff changeset
30 they are all pointers; if you bind to variables, they're values.
kono
parents:
diff changeset
31 fmt.Println("ip has value ", *ip)
kono
parents:
diff changeset
32 fmt.Println("flagvar has value ", flagvar)
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 After parsing, the arguments following the flags are available as the
kono
parents:
diff changeset
35 slice flag.Args() or individually as flag.Arg(i).
kono
parents:
diff changeset
36 The arguments are indexed from 0 through flag.NArg()-1.
kono
parents:
diff changeset
37
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
38 Command line flag syntax
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
39
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
40 The following forms are permitted:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
41
111
kono
parents:
diff changeset
42 -flag
kono
parents:
diff changeset
43 -flag=x
kono
parents:
diff changeset
44 -flag x // non-boolean flags only
kono
parents:
diff changeset
45 One or two minus signs may be used; they are equivalent.
kono
parents:
diff changeset
46 The last form is not permitted for boolean flags because the
kono
parents:
diff changeset
47 meaning of the command
kono
parents:
diff changeset
48 cmd -x *
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
49 where * is a Unix shell wildcard, will change if there is a file
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
50 called 0, false, etc. You must use the -flag=false form to turn
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
51 off a boolean flag.
111
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 Flag parsing stops just before the first non-flag argument
kono
parents:
diff changeset
54 ("-" is a non-flag argument) or after the terminator "--".
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 Integer flags accept 1234, 0664, 0x1234 and may be negative.
kono
parents:
diff changeset
57 Boolean flags may be:
kono
parents:
diff changeset
58 1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
kono
parents:
diff changeset
59 Duration flags accept any input valid for time.ParseDuration.
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 The default set of command-line flags is controlled by
kono
parents:
diff changeset
62 top-level functions. The FlagSet type allows one to define
kono
parents:
diff changeset
63 independent sets of flags, such as to implement subcommands
kono
parents:
diff changeset
64 in a command-line interface. The methods of FlagSet are
kono
parents:
diff changeset
65 analogous to the top-level functions for the command-line
kono
parents:
diff changeset
66 flag set.
kono
parents:
diff changeset
67 */
kono
parents:
diff changeset
68 package flag
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 import (
kono
parents:
diff changeset
71 "errors"
kono
parents:
diff changeset
72 "fmt"
kono
parents:
diff changeset
73 "io"
kono
parents:
diff changeset
74 "os"
kono
parents:
diff changeset
75 "reflect"
kono
parents:
diff changeset
76 "sort"
kono
parents:
diff changeset
77 "strconv"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
78 "strings"
111
kono
parents:
diff changeset
79 "time"
kono
parents:
diff changeset
80 )
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 // ErrHelp is the error returned if the -help or -h flag is invoked
kono
parents:
diff changeset
83 // but no such flag is defined.
kono
parents:
diff changeset
84 var ErrHelp = errors.New("flag: help requested")
kono
parents:
diff changeset
85
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
86 // errParse is returned by Set if a flag's value fails to parse, such as with an invalid integer for Int.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
87 // It then gets wrapped through failf to provide more information.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
88 var errParse = errors.New("parse error")
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
89
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
90 // errRange is returned by Set if a flag's value is out of range.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
91 // It then gets wrapped through failf to provide more information.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
92 var errRange = errors.New("value out of range")
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
93
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
94 func numError(err error) error {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
95 ne, ok := err.(*strconv.NumError)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
96 if !ok {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
97 return err
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
98 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
99 if ne.Err == strconv.ErrSyntax {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
100 return errParse
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
101 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
102 if ne.Err == strconv.ErrRange {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
103 return errRange
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
104 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
105 return err
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
106 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
107
111
kono
parents:
diff changeset
108 // -- bool Value
kono
parents:
diff changeset
109 type boolValue bool
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 func newBoolValue(val bool, p *bool) *boolValue {
kono
parents:
diff changeset
112 *p = val
kono
parents:
diff changeset
113 return (*boolValue)(p)
kono
parents:
diff changeset
114 }
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 func (b *boolValue) Set(s string) error {
kono
parents:
diff changeset
117 v, err := strconv.ParseBool(s)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
118 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
119 err = errParse
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
120 }
111
kono
parents:
diff changeset
121 *b = boolValue(v)
kono
parents:
diff changeset
122 return err
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 func (b *boolValue) Get() interface{} { return bool(*b) }
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 func (b *boolValue) IsBoolFlag() bool { return true }
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 // optional interface to indicate boolean flags that can be
kono
parents:
diff changeset
132 // supplied without "=value" text
kono
parents:
diff changeset
133 type boolFlag interface {
kono
parents:
diff changeset
134 Value
kono
parents:
diff changeset
135 IsBoolFlag() bool
kono
parents:
diff changeset
136 }
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 // -- int Value
kono
parents:
diff changeset
139 type intValue int
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 func newIntValue(val int, p *int) *intValue {
kono
parents:
diff changeset
142 *p = val
kono
parents:
diff changeset
143 return (*intValue)(p)
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 func (i *intValue) Set(s string) error {
kono
parents:
diff changeset
147 v, err := strconv.ParseInt(s, 0, strconv.IntSize)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
148 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
149 err = numError(err)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
150 }
111
kono
parents:
diff changeset
151 *i = intValue(v)
kono
parents:
diff changeset
152 return err
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 func (i *intValue) Get() interface{} { return int(*i) }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 func (i *intValue) String() string { return strconv.Itoa(int(*i)) }
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 // -- int64 Value
kono
parents:
diff changeset
160 type int64Value int64
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 func newInt64Value(val int64, p *int64) *int64Value {
kono
parents:
diff changeset
163 *p = val
kono
parents:
diff changeset
164 return (*int64Value)(p)
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 func (i *int64Value) Set(s string) error {
kono
parents:
diff changeset
168 v, err := strconv.ParseInt(s, 0, 64)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
169 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
170 err = numError(err)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
171 }
111
kono
parents:
diff changeset
172 *i = int64Value(v)
kono
parents:
diff changeset
173 return err
kono
parents:
diff changeset
174 }
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 func (i *int64Value) Get() interface{} { return int64(*i) }
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 // -- uint Value
kono
parents:
diff changeset
181 type uintValue uint
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 func newUintValue(val uint, p *uint) *uintValue {
kono
parents:
diff changeset
184 *p = val
kono
parents:
diff changeset
185 return (*uintValue)(p)
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 func (i *uintValue) Set(s string) error {
kono
parents:
diff changeset
189 v, err := strconv.ParseUint(s, 0, strconv.IntSize)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
190 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
191 err = numError(err)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
192 }
111
kono
parents:
diff changeset
193 *i = uintValue(v)
kono
parents:
diff changeset
194 return err
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 func (i *uintValue) Get() interface{} { return uint(*i) }
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) }
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 // -- uint64 Value
kono
parents:
diff changeset
202 type uint64Value uint64
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 func newUint64Value(val uint64, p *uint64) *uint64Value {
kono
parents:
diff changeset
205 *p = val
kono
parents:
diff changeset
206 return (*uint64Value)(p)
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 func (i *uint64Value) Set(s string) error {
kono
parents:
diff changeset
210 v, err := strconv.ParseUint(s, 0, 64)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
211 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
212 err = numError(err)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
213 }
111
kono
parents:
diff changeset
214 *i = uint64Value(v)
kono
parents:
diff changeset
215 return err
kono
parents:
diff changeset
216 }
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 func (i *uint64Value) Get() interface{} { return uint64(*i) }
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 func (i *uint64Value) String() string { return strconv.FormatUint(uint64(*i), 10) }
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 // -- string Value
kono
parents:
diff changeset
223 type stringValue string
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 func newStringValue(val string, p *string) *stringValue {
kono
parents:
diff changeset
226 *p = val
kono
parents:
diff changeset
227 return (*stringValue)(p)
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 func (s *stringValue) Set(val string) error {
kono
parents:
diff changeset
231 *s = stringValue(val)
kono
parents:
diff changeset
232 return nil
kono
parents:
diff changeset
233 }
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 func (s *stringValue) Get() interface{} { return string(*s) }
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 func (s *stringValue) String() string { return string(*s) }
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 // -- float64 Value
kono
parents:
diff changeset
240 type float64Value float64
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 func newFloat64Value(val float64, p *float64) *float64Value {
kono
parents:
diff changeset
243 *p = val
kono
parents:
diff changeset
244 return (*float64Value)(p)
kono
parents:
diff changeset
245 }
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 func (f *float64Value) Set(s string) error {
kono
parents:
diff changeset
248 v, err := strconv.ParseFloat(s, 64)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
249 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
250 err = numError(err)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
251 }
111
kono
parents:
diff changeset
252 *f = float64Value(v)
kono
parents:
diff changeset
253 return err
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 func (f *float64Value) Get() interface{} { return float64(*f) }
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 func (f *float64Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 // -- time.Duration Value
kono
parents:
diff changeset
261 type durationValue time.Duration
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
kono
parents:
diff changeset
264 *p = val
kono
parents:
diff changeset
265 return (*durationValue)(p)
kono
parents:
diff changeset
266 }
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 func (d *durationValue) Set(s string) error {
kono
parents:
diff changeset
269 v, err := time.ParseDuration(s)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
270 if err != nil {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
271 err = errParse
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
272 }
111
kono
parents:
diff changeset
273 *d = durationValue(v)
kono
parents:
diff changeset
274 return err
kono
parents:
diff changeset
275 }
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 func (d *durationValue) Get() interface{} { return time.Duration(*d) }
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 func (d *durationValue) String() string { return (*time.Duration)(d).String() }
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 // Value is the interface to the dynamic value stored in a flag.
kono
parents:
diff changeset
282 // (The default value is represented as a string.)
kono
parents:
diff changeset
283 //
kono
parents:
diff changeset
284 // If a Value has an IsBoolFlag() bool method returning true,
kono
parents:
diff changeset
285 // the command-line parser makes -name equivalent to -name=true
kono
parents:
diff changeset
286 // rather than using the next command-line argument.
kono
parents:
diff changeset
287 //
kono
parents:
diff changeset
288 // Set is called once, in command line order, for each flag present.
kono
parents:
diff changeset
289 // The flag package may call the String method with a zero-valued receiver,
kono
parents:
diff changeset
290 // such as a nil pointer.
kono
parents:
diff changeset
291 type Value interface {
kono
parents:
diff changeset
292 String() string
kono
parents:
diff changeset
293 Set(string) error
kono
parents:
diff changeset
294 }
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 // Getter is an interface that allows the contents of a Value to be retrieved.
kono
parents:
diff changeset
297 // It wraps the Value interface, rather than being part of it, because it
kono
parents:
diff changeset
298 // appeared after Go 1 and its compatibility rules. All Value types provided
kono
parents:
diff changeset
299 // by this package satisfy the Getter interface.
kono
parents:
diff changeset
300 type Getter interface {
kono
parents:
diff changeset
301 Value
kono
parents:
diff changeset
302 Get() interface{}
kono
parents:
diff changeset
303 }
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 // ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
kono
parents:
diff changeset
306 type ErrorHandling int
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 // These constants cause FlagSet.Parse to behave as described if the parse fails.
kono
parents:
diff changeset
309 const (
kono
parents:
diff changeset
310 ContinueOnError ErrorHandling = iota // Return a descriptive error.
kono
parents:
diff changeset
311 ExitOnError // Call os.Exit(2).
kono
parents:
diff changeset
312 PanicOnError // Call panic with a descriptive error.
kono
parents:
diff changeset
313 )
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 // A FlagSet represents a set of defined flags. The zero value of a FlagSet
kono
parents:
diff changeset
316 // has no name and has ContinueOnError error handling.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
317 //
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
318 // Flag names must be unique within a FlagSet. An attempt to define a flag whose
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
319 // name is already in use will cause a panic.
111
kono
parents:
diff changeset
320 type FlagSet struct {
kono
parents:
diff changeset
321 // Usage is the function called when an error occurs while parsing flags.
kono
parents:
diff changeset
322 // The field is a function (not a method) that may be changed to point to
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
323 // a custom error handler. What happens after Usage is called depends
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
324 // on the ErrorHandling setting; for the command line, this defaults
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
325 // to ExitOnError, which exits the program after calling Usage.
111
kono
parents:
diff changeset
326 Usage func()
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 name string
kono
parents:
diff changeset
329 parsed bool
kono
parents:
diff changeset
330 actual map[string]*Flag
kono
parents:
diff changeset
331 formal map[string]*Flag
kono
parents:
diff changeset
332 args []string // arguments after flags
kono
parents:
diff changeset
333 errorHandling ErrorHandling
kono
parents:
diff changeset
334 output io.Writer // nil means stderr; use out() accessor
kono
parents:
diff changeset
335 }
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 // A Flag represents the state of a flag.
kono
parents:
diff changeset
338 type Flag struct {
kono
parents:
diff changeset
339 Name string // name as it appears on command line
kono
parents:
diff changeset
340 Usage string // help message
kono
parents:
diff changeset
341 Value Value // value as set
kono
parents:
diff changeset
342 DefValue string // default value (as text); for usage message
kono
parents:
diff changeset
343 }
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 // sortFlags returns the flags as a slice in lexicographical sorted order.
kono
parents:
diff changeset
346 func sortFlags(flags map[string]*Flag) []*Flag {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
347 result := make([]*Flag, len(flags))
111
kono
parents:
diff changeset
348 i := 0
kono
parents:
diff changeset
349 for _, f := range flags {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
350 result[i] = f
111
kono
parents:
diff changeset
351 i++
kono
parents:
diff changeset
352 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
353 sort.Slice(result, func(i, j int) bool {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
354 return result[i].Name < result[j].Name
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
355 })
111
kono
parents:
diff changeset
356 return result
kono
parents:
diff changeset
357 }
kono
parents:
diff changeset
358
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
359 // Output returns the destination for usage and error messages. os.Stderr is returned if
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
360 // output was not set or was set to nil.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
361 func (f *FlagSet) Output() io.Writer {
111
kono
parents:
diff changeset
362 if f.output == nil {
kono
parents:
diff changeset
363 return os.Stderr
kono
parents:
diff changeset
364 }
kono
parents:
diff changeset
365 return f.output
kono
parents:
diff changeset
366 }
kono
parents:
diff changeset
367
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
368 // Name returns the name of the flag set.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
369 func (f *FlagSet) Name() string {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
370 return f.name
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
371 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
372
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
373 // ErrorHandling returns the error handling behavior of the flag set.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
374 func (f *FlagSet) ErrorHandling() ErrorHandling {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
375 return f.errorHandling
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
376 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
377
111
kono
parents:
diff changeset
378 // SetOutput sets the destination for usage and error messages.
kono
parents:
diff changeset
379 // If output is nil, os.Stderr is used.
kono
parents:
diff changeset
380 func (f *FlagSet) SetOutput(output io.Writer) {
kono
parents:
diff changeset
381 f.output = output
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 // VisitAll visits the flags in lexicographical order, calling fn for each.
kono
parents:
diff changeset
385 // It visits all flags, even those not set.
kono
parents:
diff changeset
386 func (f *FlagSet) VisitAll(fn func(*Flag)) {
kono
parents:
diff changeset
387 for _, flag := range sortFlags(f.formal) {
kono
parents:
diff changeset
388 fn(flag)
kono
parents:
diff changeset
389 }
kono
parents:
diff changeset
390 }
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 // VisitAll visits the command-line flags in lexicographical order, calling
kono
parents:
diff changeset
393 // fn for each. It visits all flags, even those not set.
kono
parents:
diff changeset
394 func VisitAll(fn func(*Flag)) {
kono
parents:
diff changeset
395 CommandLine.VisitAll(fn)
kono
parents:
diff changeset
396 }
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 // Visit visits the flags in lexicographical order, calling fn for each.
kono
parents:
diff changeset
399 // It visits only those flags that have been set.
kono
parents:
diff changeset
400 func (f *FlagSet) Visit(fn func(*Flag)) {
kono
parents:
diff changeset
401 for _, flag := range sortFlags(f.actual) {
kono
parents:
diff changeset
402 fn(flag)
kono
parents:
diff changeset
403 }
kono
parents:
diff changeset
404 }
kono
parents:
diff changeset
405
kono
parents:
diff changeset
406 // Visit visits the command-line flags in lexicographical order, calling fn
kono
parents:
diff changeset
407 // for each. It visits only those flags that have been set.
kono
parents:
diff changeset
408 func Visit(fn func(*Flag)) {
kono
parents:
diff changeset
409 CommandLine.Visit(fn)
kono
parents:
diff changeset
410 }
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 // Lookup returns the Flag structure of the named flag, returning nil if none exists.
kono
parents:
diff changeset
413 func (f *FlagSet) Lookup(name string) *Flag {
kono
parents:
diff changeset
414 return f.formal[name]
kono
parents:
diff changeset
415 }
kono
parents:
diff changeset
416
kono
parents:
diff changeset
417 // Lookup returns the Flag structure of the named command-line flag,
kono
parents:
diff changeset
418 // returning nil if none exists.
kono
parents:
diff changeset
419 func Lookup(name string) *Flag {
kono
parents:
diff changeset
420 return CommandLine.formal[name]
kono
parents:
diff changeset
421 }
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 // Set sets the value of the named flag.
kono
parents:
diff changeset
424 func (f *FlagSet) Set(name, value string) error {
kono
parents:
diff changeset
425 flag, ok := f.formal[name]
kono
parents:
diff changeset
426 if !ok {
kono
parents:
diff changeset
427 return fmt.Errorf("no such flag -%v", name)
kono
parents:
diff changeset
428 }
kono
parents:
diff changeset
429 err := flag.Value.Set(value)
kono
parents:
diff changeset
430 if err != nil {
kono
parents:
diff changeset
431 return err
kono
parents:
diff changeset
432 }
kono
parents:
diff changeset
433 if f.actual == nil {
kono
parents:
diff changeset
434 f.actual = make(map[string]*Flag)
kono
parents:
diff changeset
435 }
kono
parents:
diff changeset
436 f.actual[name] = flag
kono
parents:
diff changeset
437 return nil
kono
parents:
diff changeset
438 }
kono
parents:
diff changeset
439
kono
parents:
diff changeset
440 // Set sets the value of the named command-line flag.
kono
parents:
diff changeset
441 func Set(name, value string) error {
kono
parents:
diff changeset
442 return CommandLine.Set(name, value)
kono
parents:
diff changeset
443 }
kono
parents:
diff changeset
444
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
445 // isZeroValue determines whether the string represents the zero
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
446 // value for a flag.
111
kono
parents:
diff changeset
447 func isZeroValue(flag *Flag, value string) bool {
kono
parents:
diff changeset
448 // Build a zero value of the flag's Value type, and see if the
kono
parents:
diff changeset
449 // result of calling its String method equals the value passed in.
kono
parents:
diff changeset
450 // This works unless the Value type is itself an interface type.
kono
parents:
diff changeset
451 typ := reflect.TypeOf(flag.Value)
kono
parents:
diff changeset
452 var z reflect.Value
kono
parents:
diff changeset
453 if typ.Kind() == reflect.Ptr {
kono
parents:
diff changeset
454 z = reflect.New(typ.Elem())
kono
parents:
diff changeset
455 } else {
kono
parents:
diff changeset
456 z = reflect.Zero(typ)
kono
parents:
diff changeset
457 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 return value == z.Interface().(Value).String()
111
kono
parents:
diff changeset
459 }
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 // UnquoteUsage extracts a back-quoted name from the usage
kono
parents:
diff changeset
462 // string for a flag and returns it and the un-quoted usage.
kono
parents:
diff changeset
463 // Given "a `name` to show" it returns ("name", "a name to show").
kono
parents:
diff changeset
464 // If there are no back quotes, the name is an educated guess of the
kono
parents:
diff changeset
465 // type of the flag's value, or the empty string if the flag is boolean.
kono
parents:
diff changeset
466 func UnquoteUsage(flag *Flag) (name string, usage string) {
kono
parents:
diff changeset
467 // Look for a back-quoted name, but avoid the strings package.
kono
parents:
diff changeset
468 usage = flag.Usage
kono
parents:
diff changeset
469 for i := 0; i < len(usage); i++ {
kono
parents:
diff changeset
470 if usage[i] == '`' {
kono
parents:
diff changeset
471 for j := i + 1; j < len(usage); j++ {
kono
parents:
diff changeset
472 if usage[j] == '`' {
kono
parents:
diff changeset
473 name = usage[i+1 : j]
kono
parents:
diff changeset
474 usage = usage[:i] + name + usage[j+1:]
kono
parents:
diff changeset
475 return name, usage
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477 }
kono
parents:
diff changeset
478 break // Only one back quote; use type name.
kono
parents:
diff changeset
479 }
kono
parents:
diff changeset
480 }
kono
parents:
diff changeset
481 // No explicit name, so use type if we can find one.
kono
parents:
diff changeset
482 name = "value"
kono
parents:
diff changeset
483 switch flag.Value.(type) {
kono
parents:
diff changeset
484 case boolFlag:
kono
parents:
diff changeset
485 name = ""
kono
parents:
diff changeset
486 case *durationValue:
kono
parents:
diff changeset
487 name = "duration"
kono
parents:
diff changeset
488 case *float64Value:
kono
parents:
diff changeset
489 name = "float"
kono
parents:
diff changeset
490 case *intValue, *int64Value:
kono
parents:
diff changeset
491 name = "int"
kono
parents:
diff changeset
492 case *stringValue:
kono
parents:
diff changeset
493 name = "string"
kono
parents:
diff changeset
494 case *uintValue, *uint64Value:
kono
parents:
diff changeset
495 name = "uint"
kono
parents:
diff changeset
496 }
kono
parents:
diff changeset
497 return
kono
parents:
diff changeset
498 }
kono
parents:
diff changeset
499
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
500 // PrintDefaults prints, to standard error unless configured otherwise, the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 // default values of all defined command-line flags in the set. See the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 // documentation for the global function PrintDefaults for more information.
111
kono
parents:
diff changeset
503 func (f *FlagSet) PrintDefaults() {
kono
parents:
diff changeset
504 f.VisitAll(func(flag *Flag) {
kono
parents:
diff changeset
505 s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments.
kono
parents:
diff changeset
506 name, usage := UnquoteUsage(flag)
kono
parents:
diff changeset
507 if len(name) > 0 {
kono
parents:
diff changeset
508 s += " " + name
kono
parents:
diff changeset
509 }
kono
parents:
diff changeset
510 // Boolean flags of one ASCII letter are so common we
kono
parents:
diff changeset
511 // treat them specially, putting their usage on the same line.
kono
parents:
diff changeset
512 if len(s) <= 4 { // space, space, '-', 'x'.
kono
parents:
diff changeset
513 s += "\t"
kono
parents:
diff changeset
514 } else {
kono
parents:
diff changeset
515 // Four spaces before the tab triggers good alignment
kono
parents:
diff changeset
516 // for both 4- and 8-space tab stops.
kono
parents:
diff changeset
517 s += "\n \t"
kono
parents:
diff changeset
518 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
519 s += strings.ReplaceAll(usage, "\n", "\n \t")
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
520
111
kono
parents:
diff changeset
521 if !isZeroValue(flag, flag.DefValue) {
kono
parents:
diff changeset
522 if _, ok := flag.Value.(*stringValue); ok {
kono
parents:
diff changeset
523 // put quotes on the value
kono
parents:
diff changeset
524 s += fmt.Sprintf(" (default %q)", flag.DefValue)
kono
parents:
diff changeset
525 } else {
kono
parents:
diff changeset
526 s += fmt.Sprintf(" (default %v)", flag.DefValue)
kono
parents:
diff changeset
527 }
kono
parents:
diff changeset
528 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
529 fmt.Fprint(f.Output(), s, "\n")
111
kono
parents:
diff changeset
530 })
kono
parents:
diff changeset
531 }
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 // PrintDefaults prints, to standard error unless configured otherwise,
kono
parents:
diff changeset
534 // a usage message showing the default settings of all defined
kono
parents:
diff changeset
535 // command-line flags.
kono
parents:
diff changeset
536 // For an integer valued flag x, the default output has the form
kono
parents:
diff changeset
537 // -x int
kono
parents:
diff changeset
538 // usage-message-for-x (default 7)
kono
parents:
diff changeset
539 // The usage message will appear on a separate line for anything but
kono
parents:
diff changeset
540 // a bool flag with a one-byte name. For bool flags, the type is
kono
parents:
diff changeset
541 // omitted and if the flag name is one byte the usage message appears
kono
parents:
diff changeset
542 // on the same line. The parenthetical default is omitted if the
kono
parents:
diff changeset
543 // default is the zero value for the type. The listed type, here int,
kono
parents:
diff changeset
544 // can be changed by placing a back-quoted name in the flag's usage
kono
parents:
diff changeset
545 // string; the first such item in the message is taken to be a parameter
kono
parents:
diff changeset
546 // name to show in the message and the back quotes are stripped from
kono
parents:
diff changeset
547 // the message when displayed. For instance, given
kono
parents:
diff changeset
548 // flag.String("I", "", "search `directory` for include files")
kono
parents:
diff changeset
549 // the output will be
kono
parents:
diff changeset
550 // -I directory
kono
parents:
diff changeset
551 // search directory for include files.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
552 //
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
553 // To change the destination for flag messages, call CommandLine.SetOutput.
111
kono
parents:
diff changeset
554 func PrintDefaults() {
kono
parents:
diff changeset
555 CommandLine.PrintDefaults()
kono
parents:
diff changeset
556 }
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 // defaultUsage is the default function to print a usage message.
kono
parents:
diff changeset
559 func (f *FlagSet) defaultUsage() {
kono
parents:
diff changeset
560 if f.name == "" {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
561 fmt.Fprintf(f.Output(), "Usage:\n")
111
kono
parents:
diff changeset
562 } else {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
563 fmt.Fprintf(f.Output(), "Usage of %s:\n", f.name)
111
kono
parents:
diff changeset
564 }
kono
parents:
diff changeset
565 f.PrintDefaults()
kono
parents:
diff changeset
566 }
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 // NOTE: Usage is not just defaultUsage(CommandLine)
kono
parents:
diff changeset
569 // because it serves (via godoc flag Usage) as the example
kono
parents:
diff changeset
570 // for how to write your own usage function.
kono
parents:
diff changeset
571
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
572 // Usage prints a usage message documenting all defined command-line flags
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
573 // to CommandLine's output, which by default is os.Stderr.
111
kono
parents:
diff changeset
574 // It is called when an error occurs while parsing flags.
kono
parents:
diff changeset
575 // The function is a variable that may be changed to point to a custom function.
kono
parents:
diff changeset
576 // By default it prints a simple header and calls PrintDefaults; for details about the
kono
parents:
diff changeset
577 // format of the output and how to control it, see the documentation for PrintDefaults.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
578 // Custom usage functions may choose to exit the program; by default exiting
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
579 // happens anyway as the command line's error handling strategy is set to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
580 // ExitOnError.
111
kono
parents:
diff changeset
581 var Usage = func() {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
582 fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
111
kono
parents:
diff changeset
583 PrintDefaults()
kono
parents:
diff changeset
584 }
kono
parents:
diff changeset
585
kono
parents:
diff changeset
586 // NFlag returns the number of flags that have been set.
kono
parents:
diff changeset
587 func (f *FlagSet) NFlag() int { return len(f.actual) }
kono
parents:
diff changeset
588
kono
parents:
diff changeset
589 // NFlag returns the number of command-line flags that have been set.
kono
parents:
diff changeset
590 func NFlag() int { return len(CommandLine.actual) }
kono
parents:
diff changeset
591
kono
parents:
diff changeset
592 // Arg returns the i'th argument. Arg(0) is the first remaining argument
kono
parents:
diff changeset
593 // after flags have been processed. Arg returns an empty string if the
kono
parents:
diff changeset
594 // requested element does not exist.
kono
parents:
diff changeset
595 func (f *FlagSet) Arg(i int) string {
kono
parents:
diff changeset
596 if i < 0 || i >= len(f.args) {
kono
parents:
diff changeset
597 return ""
kono
parents:
diff changeset
598 }
kono
parents:
diff changeset
599 return f.args[i]
kono
parents:
diff changeset
600 }
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 // Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
kono
parents:
diff changeset
603 // after flags have been processed. Arg returns an empty string if the
kono
parents:
diff changeset
604 // requested element does not exist.
kono
parents:
diff changeset
605 func Arg(i int) string {
kono
parents:
diff changeset
606 return CommandLine.Arg(i)
kono
parents:
diff changeset
607 }
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 // NArg is the number of arguments remaining after flags have been processed.
kono
parents:
diff changeset
610 func (f *FlagSet) NArg() int { return len(f.args) }
kono
parents:
diff changeset
611
kono
parents:
diff changeset
612 // NArg is the number of arguments remaining after flags have been processed.
kono
parents:
diff changeset
613 func NArg() int { return len(CommandLine.args) }
kono
parents:
diff changeset
614
kono
parents:
diff changeset
615 // Args returns the non-flag arguments.
kono
parents:
diff changeset
616 func (f *FlagSet) Args() []string { return f.args }
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 // Args returns the non-flag command-line arguments.
kono
parents:
diff changeset
619 func Args() []string { return CommandLine.args }
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 // BoolVar defines a bool flag with specified name, default value, and usage string.
kono
parents:
diff changeset
622 // The argument p points to a bool variable in which to store the value of the flag.
kono
parents:
diff changeset
623 func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
kono
parents:
diff changeset
624 f.Var(newBoolValue(value, p), name, usage)
kono
parents:
diff changeset
625 }
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 // BoolVar defines a bool flag with specified name, default value, and usage string.
kono
parents:
diff changeset
628 // The argument p points to a bool variable in which to store the value of the flag.
kono
parents:
diff changeset
629 func BoolVar(p *bool, name string, value bool, usage string) {
kono
parents:
diff changeset
630 CommandLine.Var(newBoolValue(value, p), name, usage)
kono
parents:
diff changeset
631 }
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 // Bool defines a bool flag with specified name, default value, and usage string.
kono
parents:
diff changeset
634 // The return value is the address of a bool variable that stores the value of the flag.
kono
parents:
diff changeset
635 func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
kono
parents:
diff changeset
636 p := new(bool)
kono
parents:
diff changeset
637 f.BoolVar(p, name, value, usage)
kono
parents:
diff changeset
638 return p
kono
parents:
diff changeset
639 }
kono
parents:
diff changeset
640
kono
parents:
diff changeset
641 // Bool defines a bool flag with specified name, default value, and usage string.
kono
parents:
diff changeset
642 // The return value is the address of a bool variable that stores the value of the flag.
kono
parents:
diff changeset
643 func Bool(name string, value bool, usage string) *bool {
kono
parents:
diff changeset
644 return CommandLine.Bool(name, value, usage)
kono
parents:
diff changeset
645 }
kono
parents:
diff changeset
646
kono
parents:
diff changeset
647 // IntVar defines an int flag with specified name, default value, and usage string.
kono
parents:
diff changeset
648 // The argument p points to an int variable in which to store the value of the flag.
kono
parents:
diff changeset
649 func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
kono
parents:
diff changeset
650 f.Var(newIntValue(value, p), name, usage)
kono
parents:
diff changeset
651 }
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 // IntVar defines an int flag with specified name, default value, and usage string.
kono
parents:
diff changeset
654 // The argument p points to an int variable in which to store the value of the flag.
kono
parents:
diff changeset
655 func IntVar(p *int, name string, value int, usage string) {
kono
parents:
diff changeset
656 CommandLine.Var(newIntValue(value, p), name, usage)
kono
parents:
diff changeset
657 }
kono
parents:
diff changeset
658
kono
parents:
diff changeset
659 // Int defines an int flag with specified name, default value, and usage string.
kono
parents:
diff changeset
660 // The return value is the address of an int variable that stores the value of the flag.
kono
parents:
diff changeset
661 func (f *FlagSet) Int(name string, value int, usage string) *int {
kono
parents:
diff changeset
662 p := new(int)
kono
parents:
diff changeset
663 f.IntVar(p, name, value, usage)
kono
parents:
diff changeset
664 return p
kono
parents:
diff changeset
665 }
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 // Int defines an int flag with specified name, default value, and usage string.
kono
parents:
diff changeset
668 // The return value is the address of an int variable that stores the value of the flag.
kono
parents:
diff changeset
669 func Int(name string, value int, usage string) *int {
kono
parents:
diff changeset
670 return CommandLine.Int(name, value, usage)
kono
parents:
diff changeset
671 }
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 // Int64Var defines an int64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
674 // The argument p points to an int64 variable in which to store the value of the flag.
kono
parents:
diff changeset
675 func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
kono
parents:
diff changeset
676 f.Var(newInt64Value(value, p), name, usage)
kono
parents:
diff changeset
677 }
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 // Int64Var defines an int64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
680 // The argument p points to an int64 variable in which to store the value of the flag.
kono
parents:
diff changeset
681 func Int64Var(p *int64, name string, value int64, usage string) {
kono
parents:
diff changeset
682 CommandLine.Var(newInt64Value(value, p), name, usage)
kono
parents:
diff changeset
683 }
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 // Int64 defines an int64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
686 // The return value is the address of an int64 variable that stores the value of the flag.
kono
parents:
diff changeset
687 func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
kono
parents:
diff changeset
688 p := new(int64)
kono
parents:
diff changeset
689 f.Int64Var(p, name, value, usage)
kono
parents:
diff changeset
690 return p
kono
parents:
diff changeset
691 }
kono
parents:
diff changeset
692
kono
parents:
diff changeset
693 // Int64 defines an int64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
694 // The return value is the address of an int64 variable that stores the value of the flag.
kono
parents:
diff changeset
695 func Int64(name string, value int64, usage string) *int64 {
kono
parents:
diff changeset
696 return CommandLine.Int64(name, value, usage)
kono
parents:
diff changeset
697 }
kono
parents:
diff changeset
698
kono
parents:
diff changeset
699 // UintVar defines a uint flag with specified name, default value, and usage string.
kono
parents:
diff changeset
700 // The argument p points to a uint variable in which to store the value of the flag.
kono
parents:
diff changeset
701 func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
kono
parents:
diff changeset
702 f.Var(newUintValue(value, p), name, usage)
kono
parents:
diff changeset
703 }
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 // UintVar defines a uint flag with specified name, default value, and usage string.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
706 // The argument p points to a uint variable in which to store the value of the flag.
111
kono
parents:
diff changeset
707 func UintVar(p *uint, name string, value uint, usage string) {
kono
parents:
diff changeset
708 CommandLine.Var(newUintValue(value, p), name, usage)
kono
parents:
diff changeset
709 }
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 // Uint defines a uint flag with specified name, default value, and usage string.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
712 // The return value is the address of a uint variable that stores the value of the flag.
111
kono
parents:
diff changeset
713 func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
kono
parents:
diff changeset
714 p := new(uint)
kono
parents:
diff changeset
715 f.UintVar(p, name, value, usage)
kono
parents:
diff changeset
716 return p
kono
parents:
diff changeset
717 }
kono
parents:
diff changeset
718
kono
parents:
diff changeset
719 // Uint defines a uint flag with specified name, default value, and usage string.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
720 // The return value is the address of a uint variable that stores the value of the flag.
111
kono
parents:
diff changeset
721 func Uint(name string, value uint, usage string) *uint {
kono
parents:
diff changeset
722 return CommandLine.Uint(name, value, usage)
kono
parents:
diff changeset
723 }
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
726 // The argument p points to a uint64 variable in which to store the value of the flag.
kono
parents:
diff changeset
727 func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
kono
parents:
diff changeset
728 f.Var(newUint64Value(value, p), name, usage)
kono
parents:
diff changeset
729 }
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
732 // The argument p points to a uint64 variable in which to store the value of the flag.
kono
parents:
diff changeset
733 func Uint64Var(p *uint64, name string, value uint64, usage string) {
kono
parents:
diff changeset
734 CommandLine.Var(newUint64Value(value, p), name, usage)
kono
parents:
diff changeset
735 }
kono
parents:
diff changeset
736
kono
parents:
diff changeset
737 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
738 // The return value is the address of a uint64 variable that stores the value of the flag.
kono
parents:
diff changeset
739 func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
kono
parents:
diff changeset
740 p := new(uint64)
kono
parents:
diff changeset
741 f.Uint64Var(p, name, value, usage)
kono
parents:
diff changeset
742 return p
kono
parents:
diff changeset
743 }
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
746 // The return value is the address of a uint64 variable that stores the value of the flag.
kono
parents:
diff changeset
747 func Uint64(name string, value uint64, usage string) *uint64 {
kono
parents:
diff changeset
748 return CommandLine.Uint64(name, value, usage)
kono
parents:
diff changeset
749 }
kono
parents:
diff changeset
750
kono
parents:
diff changeset
751 // StringVar defines a string flag with specified name, default value, and usage string.
kono
parents:
diff changeset
752 // The argument p points to a string variable in which to store the value of the flag.
kono
parents:
diff changeset
753 func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
kono
parents:
diff changeset
754 f.Var(newStringValue(value, p), name, usage)
kono
parents:
diff changeset
755 }
kono
parents:
diff changeset
756
kono
parents:
diff changeset
757 // StringVar defines a string flag with specified name, default value, and usage string.
kono
parents:
diff changeset
758 // The argument p points to a string variable in which to store the value of the flag.
kono
parents:
diff changeset
759 func StringVar(p *string, name string, value string, usage string) {
kono
parents:
diff changeset
760 CommandLine.Var(newStringValue(value, p), name, usage)
kono
parents:
diff changeset
761 }
kono
parents:
diff changeset
762
kono
parents:
diff changeset
763 // String defines a string flag with specified name, default value, and usage string.
kono
parents:
diff changeset
764 // The return value is the address of a string variable that stores the value of the flag.
kono
parents:
diff changeset
765 func (f *FlagSet) String(name string, value string, usage string) *string {
kono
parents:
diff changeset
766 p := new(string)
kono
parents:
diff changeset
767 f.StringVar(p, name, value, usage)
kono
parents:
diff changeset
768 return p
kono
parents:
diff changeset
769 }
kono
parents:
diff changeset
770
kono
parents:
diff changeset
771 // String defines a string flag with specified name, default value, and usage string.
kono
parents:
diff changeset
772 // The return value is the address of a string variable that stores the value of the flag.
kono
parents:
diff changeset
773 func String(name string, value string, usage string) *string {
kono
parents:
diff changeset
774 return CommandLine.String(name, value, usage)
kono
parents:
diff changeset
775 }
kono
parents:
diff changeset
776
kono
parents:
diff changeset
777 // Float64Var defines a float64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
778 // The argument p points to a float64 variable in which to store the value of the flag.
kono
parents:
diff changeset
779 func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
kono
parents:
diff changeset
780 f.Var(newFloat64Value(value, p), name, usage)
kono
parents:
diff changeset
781 }
kono
parents:
diff changeset
782
kono
parents:
diff changeset
783 // Float64Var defines a float64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
784 // The argument p points to a float64 variable in which to store the value of the flag.
kono
parents:
diff changeset
785 func Float64Var(p *float64, name string, value float64, usage string) {
kono
parents:
diff changeset
786 CommandLine.Var(newFloat64Value(value, p), name, usage)
kono
parents:
diff changeset
787 }
kono
parents:
diff changeset
788
kono
parents:
diff changeset
789 // Float64 defines a float64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
790 // The return value is the address of a float64 variable that stores the value of the flag.
kono
parents:
diff changeset
791 func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
kono
parents:
diff changeset
792 p := new(float64)
kono
parents:
diff changeset
793 f.Float64Var(p, name, value, usage)
kono
parents:
diff changeset
794 return p
kono
parents:
diff changeset
795 }
kono
parents:
diff changeset
796
kono
parents:
diff changeset
797 // Float64 defines a float64 flag with specified name, default value, and usage string.
kono
parents:
diff changeset
798 // The return value is the address of a float64 variable that stores the value of the flag.
kono
parents:
diff changeset
799 func Float64(name string, value float64, usage string) *float64 {
kono
parents:
diff changeset
800 return CommandLine.Float64(name, value, usage)
kono
parents:
diff changeset
801 }
kono
parents:
diff changeset
802
kono
parents:
diff changeset
803 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
kono
parents:
diff changeset
804 // The argument p points to a time.Duration variable in which to store the value of the flag.
kono
parents:
diff changeset
805 // The flag accepts a value acceptable to time.ParseDuration.
kono
parents:
diff changeset
806 func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
kono
parents:
diff changeset
807 f.Var(newDurationValue(value, p), name, usage)
kono
parents:
diff changeset
808 }
kono
parents:
diff changeset
809
kono
parents:
diff changeset
810 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
kono
parents:
diff changeset
811 // The argument p points to a time.Duration variable in which to store the value of the flag.
kono
parents:
diff changeset
812 // The flag accepts a value acceptable to time.ParseDuration.
kono
parents:
diff changeset
813 func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
kono
parents:
diff changeset
814 CommandLine.Var(newDurationValue(value, p), name, usage)
kono
parents:
diff changeset
815 }
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 // Duration defines a time.Duration flag with specified name, default value, and usage string.
kono
parents:
diff changeset
818 // The return value is the address of a time.Duration variable that stores the value of the flag.
kono
parents:
diff changeset
819 // The flag accepts a value acceptable to time.ParseDuration.
kono
parents:
diff changeset
820 func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
kono
parents:
diff changeset
821 p := new(time.Duration)
kono
parents:
diff changeset
822 f.DurationVar(p, name, value, usage)
kono
parents:
diff changeset
823 return p
kono
parents:
diff changeset
824 }
kono
parents:
diff changeset
825
kono
parents:
diff changeset
826 // Duration defines a time.Duration flag with specified name, default value, and usage string.
kono
parents:
diff changeset
827 // The return value is the address of a time.Duration variable that stores the value of the flag.
kono
parents:
diff changeset
828 // The flag accepts a value acceptable to time.ParseDuration.
kono
parents:
diff changeset
829 func Duration(name string, value time.Duration, usage string) *time.Duration {
kono
parents:
diff changeset
830 return CommandLine.Duration(name, value, usage)
kono
parents:
diff changeset
831 }
kono
parents:
diff changeset
832
kono
parents:
diff changeset
833 // Var defines a flag with the specified name and usage string. The type and
kono
parents:
diff changeset
834 // value of the flag are represented by the first argument, of type Value, which
kono
parents:
diff changeset
835 // typically holds a user-defined implementation of Value. For instance, the
kono
parents:
diff changeset
836 // caller could create a flag that turns a comma-separated string into a slice
kono
parents:
diff changeset
837 // of strings by giving the slice the methods of Value; in particular, Set would
kono
parents:
diff changeset
838 // decompose the comma-separated string into the slice.
kono
parents:
diff changeset
839 func (f *FlagSet) Var(value Value, name string, usage string) {
kono
parents:
diff changeset
840 // Remember the default value as a string; it won't change.
kono
parents:
diff changeset
841 flag := &Flag{name, usage, value, value.String()}
kono
parents:
diff changeset
842 _, alreadythere := f.formal[name]
kono
parents:
diff changeset
843 if alreadythere {
kono
parents:
diff changeset
844 var msg string
kono
parents:
diff changeset
845 if f.name == "" {
kono
parents:
diff changeset
846 msg = fmt.Sprintf("flag redefined: %s", name)
kono
parents:
diff changeset
847 } else {
kono
parents:
diff changeset
848 msg = fmt.Sprintf("%s flag redefined: %s", f.name, name)
kono
parents:
diff changeset
849 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
850 fmt.Fprintln(f.Output(), msg)
111
kono
parents:
diff changeset
851 panic(msg) // Happens only if flags are declared with identical names
kono
parents:
diff changeset
852 }
kono
parents:
diff changeset
853 if f.formal == nil {
kono
parents:
diff changeset
854 f.formal = make(map[string]*Flag)
kono
parents:
diff changeset
855 }
kono
parents:
diff changeset
856 f.formal[name] = flag
kono
parents:
diff changeset
857 }
kono
parents:
diff changeset
858
kono
parents:
diff changeset
859 // Var defines a flag with the specified name and usage string. The type and
kono
parents:
diff changeset
860 // value of the flag are represented by the first argument, of type Value, which
kono
parents:
diff changeset
861 // typically holds a user-defined implementation of Value. For instance, the
kono
parents:
diff changeset
862 // caller could create a flag that turns a comma-separated string into a slice
kono
parents:
diff changeset
863 // of strings by giving the slice the methods of Value; in particular, Set would
kono
parents:
diff changeset
864 // decompose the comma-separated string into the slice.
kono
parents:
diff changeset
865 func Var(value Value, name string, usage string) {
kono
parents:
diff changeset
866 CommandLine.Var(value, name, usage)
kono
parents:
diff changeset
867 }
kono
parents:
diff changeset
868
kono
parents:
diff changeset
869 // failf prints to standard error a formatted error and usage message and
kono
parents:
diff changeset
870 // returns the error.
kono
parents:
diff changeset
871 func (f *FlagSet) failf(format string, a ...interface{}) error {
kono
parents:
diff changeset
872 err := fmt.Errorf(format, a...)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
873 fmt.Fprintln(f.Output(), err)
111
kono
parents:
diff changeset
874 f.usage()
kono
parents:
diff changeset
875 return err
kono
parents:
diff changeset
876 }
kono
parents:
diff changeset
877
kono
parents:
diff changeset
878 // usage calls the Usage method for the flag set if one is specified,
kono
parents:
diff changeset
879 // or the appropriate default usage function otherwise.
kono
parents:
diff changeset
880 func (f *FlagSet) usage() {
kono
parents:
diff changeset
881 if f.Usage == nil {
kono
parents:
diff changeset
882 f.defaultUsage()
kono
parents:
diff changeset
883 } else {
kono
parents:
diff changeset
884 f.Usage()
kono
parents:
diff changeset
885 }
kono
parents:
diff changeset
886 }
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 // parseOne parses one flag. It reports whether a flag was seen.
kono
parents:
diff changeset
889 func (f *FlagSet) parseOne() (bool, error) {
kono
parents:
diff changeset
890 if len(f.args) == 0 {
kono
parents:
diff changeset
891 return false, nil
kono
parents:
diff changeset
892 }
kono
parents:
diff changeset
893 s := f.args[0]
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
894 if len(s) < 2 || s[0] != '-' {
111
kono
parents:
diff changeset
895 return false, nil
kono
parents:
diff changeset
896 }
kono
parents:
diff changeset
897 numMinuses := 1
kono
parents:
diff changeset
898 if s[1] == '-' {
kono
parents:
diff changeset
899 numMinuses++
kono
parents:
diff changeset
900 if len(s) == 2 { // "--" terminates the flags
kono
parents:
diff changeset
901 f.args = f.args[1:]
kono
parents:
diff changeset
902 return false, nil
kono
parents:
diff changeset
903 }
kono
parents:
diff changeset
904 }
kono
parents:
diff changeset
905 name := s[numMinuses:]
kono
parents:
diff changeset
906 if len(name) == 0 || name[0] == '-' || name[0] == '=' {
kono
parents:
diff changeset
907 return false, f.failf("bad flag syntax: %s", s)
kono
parents:
diff changeset
908 }
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 // it's a flag. does it have an argument?
kono
parents:
diff changeset
911 f.args = f.args[1:]
kono
parents:
diff changeset
912 hasValue := false
kono
parents:
diff changeset
913 value := ""
kono
parents:
diff changeset
914 for i := 1; i < len(name); i++ { // equals cannot be first
kono
parents:
diff changeset
915 if name[i] == '=' {
kono
parents:
diff changeset
916 value = name[i+1:]
kono
parents:
diff changeset
917 hasValue = true
kono
parents:
diff changeset
918 name = name[0:i]
kono
parents:
diff changeset
919 break
kono
parents:
diff changeset
920 }
kono
parents:
diff changeset
921 }
kono
parents:
diff changeset
922 m := f.formal
kono
parents:
diff changeset
923 flag, alreadythere := m[name] // BUG
kono
parents:
diff changeset
924 if !alreadythere {
kono
parents:
diff changeset
925 if name == "help" || name == "h" { // special case for nice help message.
kono
parents:
diff changeset
926 f.usage()
kono
parents:
diff changeset
927 return false, ErrHelp
kono
parents:
diff changeset
928 }
kono
parents:
diff changeset
929 return false, f.failf("flag provided but not defined: -%s", name)
kono
parents:
diff changeset
930 }
kono
parents:
diff changeset
931
kono
parents:
diff changeset
932 if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
kono
parents:
diff changeset
933 if hasValue {
kono
parents:
diff changeset
934 if err := fv.Set(value); err != nil {
kono
parents:
diff changeset
935 return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err)
kono
parents:
diff changeset
936 }
kono
parents:
diff changeset
937 } else {
kono
parents:
diff changeset
938 if err := fv.Set("true"); err != nil {
kono
parents:
diff changeset
939 return false, f.failf("invalid boolean flag %s: %v", name, err)
kono
parents:
diff changeset
940 }
kono
parents:
diff changeset
941 }
kono
parents:
diff changeset
942 } else {
kono
parents:
diff changeset
943 // It must have a value, which might be the next argument.
kono
parents:
diff changeset
944 if !hasValue && len(f.args) > 0 {
kono
parents:
diff changeset
945 // value is the next arg
kono
parents:
diff changeset
946 hasValue = true
kono
parents:
diff changeset
947 value, f.args = f.args[0], f.args[1:]
kono
parents:
diff changeset
948 }
kono
parents:
diff changeset
949 if !hasValue {
kono
parents:
diff changeset
950 return false, f.failf("flag needs an argument: -%s", name)
kono
parents:
diff changeset
951 }
kono
parents:
diff changeset
952 if err := flag.Value.Set(value); err != nil {
kono
parents:
diff changeset
953 return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
kono
parents:
diff changeset
954 }
kono
parents:
diff changeset
955 }
kono
parents:
diff changeset
956 if f.actual == nil {
kono
parents:
diff changeset
957 f.actual = make(map[string]*Flag)
kono
parents:
diff changeset
958 }
kono
parents:
diff changeset
959 f.actual[name] = flag
kono
parents:
diff changeset
960 return true, nil
kono
parents:
diff changeset
961 }
kono
parents:
diff changeset
962
kono
parents:
diff changeset
963 // Parse parses flag definitions from the argument list, which should not
kono
parents:
diff changeset
964 // include the command name. Must be called after all flags in the FlagSet
kono
parents:
diff changeset
965 // are defined and before flags are accessed by the program.
kono
parents:
diff changeset
966 // The return value will be ErrHelp if -help or -h were set but not defined.
kono
parents:
diff changeset
967 func (f *FlagSet) Parse(arguments []string) error {
kono
parents:
diff changeset
968 f.parsed = true
kono
parents:
diff changeset
969 f.args = arguments
kono
parents:
diff changeset
970 for {
kono
parents:
diff changeset
971 seen, err := f.parseOne()
kono
parents:
diff changeset
972 if seen {
kono
parents:
diff changeset
973 continue
kono
parents:
diff changeset
974 }
kono
parents:
diff changeset
975 if err == nil {
kono
parents:
diff changeset
976 break
kono
parents:
diff changeset
977 }
kono
parents:
diff changeset
978 switch f.errorHandling {
kono
parents:
diff changeset
979 case ContinueOnError:
kono
parents:
diff changeset
980 return err
kono
parents:
diff changeset
981 case ExitOnError:
kono
parents:
diff changeset
982 os.Exit(2)
kono
parents:
diff changeset
983 case PanicOnError:
kono
parents:
diff changeset
984 panic(err)
kono
parents:
diff changeset
985 }
kono
parents:
diff changeset
986 }
kono
parents:
diff changeset
987 return nil
kono
parents:
diff changeset
988 }
kono
parents:
diff changeset
989
kono
parents:
diff changeset
990 // Parsed reports whether f.Parse has been called.
kono
parents:
diff changeset
991 func (f *FlagSet) Parsed() bool {
kono
parents:
diff changeset
992 return f.parsed
kono
parents:
diff changeset
993 }
kono
parents:
diff changeset
994
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
995 // Parse parses the command-line flags from os.Args[1:]. Must be called
111
kono
parents:
diff changeset
996 // after all flags are defined and before flags are accessed by the program.
kono
parents:
diff changeset
997 func Parse() {
kono
parents:
diff changeset
998 // Ignore errors; CommandLine is set for ExitOnError.
kono
parents:
diff changeset
999 CommandLine.Parse(os.Args[1:])
kono
parents:
diff changeset
1000 }
kono
parents:
diff changeset
1001
kono
parents:
diff changeset
1002 // Parsed reports whether the command-line flags have been parsed.
kono
parents:
diff changeset
1003 func Parsed() bool {
kono
parents:
diff changeset
1004 return CommandLine.Parsed()
kono
parents:
diff changeset
1005 }
kono
parents:
diff changeset
1006
kono
parents:
diff changeset
1007 // CommandLine is the default set of command-line flags, parsed from os.Args.
kono
parents:
diff changeset
1008 // The top-level functions such as BoolVar, Arg, and so on are wrappers for the
kono
parents:
diff changeset
1009 // methods of CommandLine.
kono
parents:
diff changeset
1010 var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 func init() {
kono
parents:
diff changeset
1013 // Override generic FlagSet default Usage with call to global Usage.
kono
parents:
diff changeset
1014 // Note: This is not CommandLine.Usage = Usage,
kono
parents:
diff changeset
1015 // because we want any eventual call to use any updated value of Usage,
kono
parents:
diff changeset
1016 // not the value it has when this line is run.
kono
parents:
diff changeset
1017 CommandLine.Usage = commandLineUsage
kono
parents:
diff changeset
1018 }
kono
parents:
diff changeset
1019
kono
parents:
diff changeset
1020 func commandLineUsage() {
kono
parents:
diff changeset
1021 Usage()
kono
parents:
diff changeset
1022 }
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 // NewFlagSet returns a new, empty flag set with the specified name and
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1025 // error handling property. If the name is not empty, it will be printed
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1026 // in the default usage message and in error messages.
111
kono
parents:
diff changeset
1027 func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
kono
parents:
diff changeset
1028 f := &FlagSet{
kono
parents:
diff changeset
1029 name: name,
kono
parents:
diff changeset
1030 errorHandling: errorHandling,
kono
parents:
diff changeset
1031 }
kono
parents:
diff changeset
1032 f.Usage = f.defaultUsage
kono
parents:
diff changeset
1033 return f
kono
parents:
diff changeset
1034 }
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 // Init sets the name and error handling property for a flag set.
kono
parents:
diff changeset
1037 // By default, the zero FlagSet uses an empty name and the
kono
parents:
diff changeset
1038 // ContinueOnError error handling policy.
kono
parents:
diff changeset
1039 func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
kono
parents:
diff changeset
1040 f.name = name
kono
parents:
diff changeset
1041 f.errorHandling = errorHandling
kono
parents:
diff changeset
1042 }