1 // Copyright 2009 The Go Authors. 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.
6 Package flag implements command-line flag parsing.
10 Define flags using flag.String(), Bool(), Int(), etc.
12 This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
14 var ip = flag.Int("flagname", 1234, "help message for flagname")
15 If you like, you can bind the flag to a variable using the Var() functions.
18 flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
20 Or you can create custom flags that satisfy the Value interface (with
21 pointer receivers) and couple them to flag parsing by
22 flag.Var(&flagVal, "name", "help message for flagname")
23 For such flags, the default value is just the initial value of the variable.
25 After all flags are defined, call
27 to parse the command line into the defined flags.
29 Flags may then be used directly. If you're using the flags themselves,
30 they are all pointers; if you bind to variables, they're values.
31 fmt.Println("ip has value ", *ip)
32 fmt.Println("flagvar has value ", flagvar)
34 After parsing, the arguments following the flags are available as the
35 slice flag.Args() or individually as flag.Arg(i).
36 The arguments are indexed from 0 through flag.NArg()-1.
38 Command line flag syntax
40 The following forms are permitted:
44 -flag x // non-boolean flags only
45 One or two minus signs may be used; they are equivalent.
46 The last form is not permitted for boolean flags because the
47 meaning of the command
49 where * is a Unix shell wildcard, will change if there is a file
50 called 0, false, etc. You must use the -flag=false form to turn
53 Flag parsing stops just before the first non-flag argument
54 ("-" is a non-flag argument) or after the terminator "--".
56 Integer flags accept 1234, 0664, 0x1234 and may be negative.
58 1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
59 Duration flags accept any input valid for time.ParseDuration.
61 The default set of command-line flags is controlled by
62 top-level functions. The FlagSet type allows one to define
63 independent sets of flags, such as to implement subcommands
64 in a command-line interface. The methods of FlagSet are
65 analogous to the top-level functions for the command-line
82 // ErrHelp is the error returned if the -help or -h flag is invoked
83 // but no such flag is defined.
84 var ErrHelp = errors.New("flag: help requested")
86 // errParse is returned by Set if a flag's value fails to parse, such as with an invalid integer for Int.
87 // It then gets wrapped through failf to provide more information.
88 var errParse = errors.New("parse error")
90 // errRange is returned by Set if a flag's value is out of range.
91 // It then gets wrapped through failf to provide more information.
92 var errRange = errors.New("value out of range")
94 func numError(err error) error {
95 ne, ok := err.(*strconv.NumError)
99 if ne.Err == strconv.ErrSyntax {
102 if ne.Err == strconv.ErrRange {
111 func newBoolValue(val bool, p *bool) *boolValue {
113 return (*boolValue)(p)
116 func (b *boolValue) Set(s string) error {
117 v, err := strconv.ParseBool(s)
125 func (b *boolValue) Get() interface{} { return bool(*b) }
127 func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
129 func (b *boolValue) IsBoolFlag() bool { return true }
131 // optional interface to indicate boolean flags that can be
132 // supplied without "=value" text
133 type boolFlag interface {
141 func newIntValue(val int, p *int) *intValue {
143 return (*intValue)(p)
146 func (i *intValue) Set(s string) error {
147 v, err := strconv.ParseInt(s, 0, strconv.IntSize)
155 func (i *intValue) Get() interface{} { return int(*i) }
157 func (i *intValue) String() string { return strconv.Itoa(int(*i)) }
160 type int64Value int64
162 func newInt64Value(val int64, p *int64) *int64Value {
164 return (*int64Value)(p)
167 func (i *int64Value) Set(s string) error {
168 v, err := strconv.ParseInt(s, 0, 64)
176 func (i *int64Value) Get() interface{} { return int64(*i) }
178 func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) }
183 func newUintValue(val uint, p *uint) *uintValue {
185 return (*uintValue)(p)
188 func (i *uintValue) Set(s string) error {
189 v, err := strconv.ParseUint(s, 0, strconv.IntSize)
197 func (i *uintValue) Get() interface{} { return uint(*i) }
199 func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) }
202 type uint64Value uint64
204 func newUint64Value(val uint64, p *uint64) *uint64Value {
206 return (*uint64Value)(p)
209 func (i *uint64Value) Set(s string) error {
210 v, err := strconv.ParseUint(s, 0, 64)
218 func (i *uint64Value) Get() interface{} { return uint64(*i) }
220 func (i *uint64Value) String() string { return strconv.FormatUint(uint64(*i), 10) }
223 type stringValue string
225 func newStringValue(val string, p *string) *stringValue {
227 return (*stringValue)(p)
230 func (s *stringValue) Set(val string) error {
231 *s = stringValue(val)
235 func (s *stringValue) Get() interface{} { return string(*s) }
237 func (s *stringValue) String() string { return string(*s) }
240 type float64Value float64
242 func newFloat64Value(val float64, p *float64) *float64Value {
244 return (*float64Value)(p)
247 func (f *float64Value) Set(s string) error {
248 v, err := strconv.ParseFloat(s, 64)
256 func (f *float64Value) Get() interface{} { return float64(*f) }
258 func (f *float64Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }
260 // -- time.Duration Value
261 type durationValue time.Duration
263 func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
265 return (*durationValue)(p)
268 func (d *durationValue) Set(s string) error {
269 v, err := time.ParseDuration(s)
273 *d = durationValue(v)
277 func (d *durationValue) Get() interface{} { return time.Duration(*d) }
279 func (d *durationValue) String() string { return (*time.Duration)(d).String() }
281 // Value is the interface to the dynamic value stored in a flag.
282 // (The default value is represented as a string.)
284 // If a Value has an IsBoolFlag() bool method returning true,
285 // the command-line parser makes -name equivalent to -name=true
286 // rather than using the next command-line argument.
288 // Set is called once, in command line order, for each flag present.
289 // The flag package may call the String method with a zero-valued receiver,
290 // such as a nil pointer.
291 type Value interface {
296 // Getter is an interface that allows the contents of a Value to be retrieved.
297 // It wraps the Value interface, rather than being part of it, because it
298 // appeared after Go 1 and its compatibility rules. All Value types provided
299 // by this package satisfy the Getter interface.
300 type Getter interface {
305 // ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
306 type ErrorHandling int
308 // These constants cause FlagSet.Parse to behave as described if the parse fails.
310 ContinueOnError ErrorHandling = iota // Return a descriptive error.
311 ExitOnError // Call os.Exit(2).
312 PanicOnError // Call panic with a descriptive error.
315 // A FlagSet represents a set of defined flags. The zero value of a FlagSet
316 // has no name and has ContinueOnError error handling.
318 // Flag names must be unique within a FlagSet. An attempt to define a flag whose
319 // name is already in use will cause a panic.
320 type FlagSet struct {
321 // Usage is the function called when an error occurs while parsing flags.
322 // The field is a function (not a method) that may be changed to point to
323 // a custom error handler. What happens after Usage is called depends
324 // on the ErrorHandling setting; for the command line, this defaults
325 // to ExitOnError, which exits the program after calling Usage.
330 actual map[string]*Flag
331 formal map[string]*Flag
332 args []string // arguments after flags
333 errorHandling ErrorHandling
334 output io.Writer // nil means stderr; use out() accessor
337 // A Flag represents the state of a flag.
339 Name string // name as it appears on command line
340 Usage string // help message
341 Value Value // value as set
342 DefValue string // default value (as text); for usage message
345 // sortFlags returns the flags as a slice in lexicographical sorted order.
346 func sortFlags(flags map[string]*Flag) []*Flag {
347 result := make([]*Flag, len(flags))
349 for _, f := range flags {
353 sort.Slice(result, func(i, j int) bool {
354 return result[i].Name < result[j].Name
359 // Output returns the destination for usage and error messages. os.Stderr is returned if
360 // output was not set or was set to nil.
361 func (f *FlagSet) Output() io.Writer {
368 // Name returns the name of the flag set.
369 func (f *FlagSet) Name() string {
373 // ErrorHandling returns the error handling behavior of the flag set.
374 func (f *FlagSet) ErrorHandling() ErrorHandling {
375 return f.errorHandling
378 // SetOutput sets the destination for usage and error messages.
379 // If output is nil, os.Stderr is used.
380 func (f *FlagSet) SetOutput(output io.Writer) {
384 // VisitAll visits the flags in lexicographical order, calling fn for each.
385 // It visits all flags, even those not set.
386 func (f *FlagSet) VisitAll(fn func(*Flag)) {
387 for _, flag := range sortFlags(f.formal) {
392 // VisitAll visits the command-line flags in lexicographical order, calling
393 // fn for each. It visits all flags, even those not set.
394 func VisitAll(fn func(*Flag)) {
395 CommandLine.VisitAll(fn)
398 // Visit visits the flags in lexicographical order, calling fn for each.
399 // It visits only those flags that have been set.
400 func (f *FlagSet) Visit(fn func(*Flag)) {
401 for _, flag := range sortFlags(f.actual) {
406 // Visit visits the command-line flags in lexicographical order, calling fn
407 // for each. It visits only those flags that have been set.
408 func Visit(fn func(*Flag)) {
409 CommandLine.Visit(fn)
412 // Lookup returns the Flag structure of the named flag, returning nil if none exists.
413 func (f *FlagSet) Lookup(name string) *Flag {
414 return f.formal[name]
417 // Lookup returns the Flag structure of the named command-line flag,
418 // returning nil if none exists.
419 func Lookup(name string) *Flag {
420 return CommandLine.formal[name]
423 // Set sets the value of the named flag.
424 func (f *FlagSet) Set(name, value string) error {
425 flag, ok := f.formal[name]
427 return fmt.Errorf("no such flag -%v", name)
429 err := flag.Value.Set(value)
434 f.actual = make(map[string]*Flag)
436 f.actual[name] = flag
440 // Set sets the value of the named command-line flag.
441 func Set(name, value string) error {
442 return CommandLine.Set(name, value)
445 // isZeroValue determines whether the string represents the zero
447 func isZeroValue(flag *Flag, value string) bool {
448 // Build a zero value of the flag's Value type, and see if the
449 // result of calling its String method equals the value passed in.
450 // This works unless the Value type is itself an interface type.
451 typ := reflect.TypeOf(flag.Value)
453 if typ.Kind() == reflect.Ptr {
454 z = reflect.New(typ.Elem())
456 z = reflect.Zero(typ)
458 return value == z.Interface().(Value).String()
461 // UnquoteUsage extracts a back-quoted name from the usage
462 // string for a flag and returns it and the un-quoted usage.
463 // Given "a `name` to show" it returns ("name", "a name to show").
464 // If there are no back quotes, the name is an educated guess of the
465 // type of the flag's value, or the empty string if the flag is boolean.
466 func UnquoteUsage(flag *Flag) (name string, usage string) {
467 // Look for a back-quoted name, but avoid the strings package.
469 for i := 0; i < len(usage); i++ {
471 for j := i + 1; j < len(usage); j++ {
473 name = usage[i+1 : j]
474 usage = usage[:i] + name + usage[j+1:]
478 break // Only one back quote; use type name.
481 // No explicit name, so use type if we can find one.
483 switch flag.Value.(type) {
490 case *intValue, *int64Value:
494 case *uintValue, *uint64Value:
500 // PrintDefaults prints, to standard error unless configured otherwise, the
501 // default values of all defined command-line flags in the set. See the
502 // documentation for the global function PrintDefaults for more information.
503 func (f *FlagSet) PrintDefaults() {
504 f.VisitAll(func(flag *Flag) {
505 s := fmt.Sprintf(" -%s", flag.Name) // Two spaces before -; see next two comments.
506 name, usage := UnquoteUsage(flag)
510 // Boolean flags of one ASCII letter are so common we
511 // treat them specially, putting their usage on the same line.
512 if len(s) <= 4 { // space, space, '-', 'x'.
515 // Four spaces before the tab triggers good alignment
516 // for both 4- and 8-space tab stops.
519 s += strings.ReplaceAll(usage, "\n", "\n \t")
521 if !isZeroValue(flag, flag.DefValue) {
522 if _, ok := flag.Value.(*stringValue); ok {
523 // put quotes on the value
524 s += fmt.Sprintf(" (default %q)", flag.DefValue)
526 s += fmt.Sprintf(" (default %v)", flag.DefValue)
529 fmt.Fprint(f.Output(), s, "\n")
533 // PrintDefaults prints, to standard error unless configured otherwise,
534 // a usage message showing the default settings of all defined
535 // command-line flags.
536 // For an integer valued flag x, the default output has the form
538 // usage-message-for-x (default 7)
539 // The usage message will appear on a separate line for anything but
540 // a bool flag with a one-byte name. For bool flags, the type is
541 // omitted and if the flag name is one byte the usage message appears
542 // on the same line. The parenthetical default is omitted if the
543 // default is the zero value for the type. The listed type, here int,
544 // can be changed by placing a back-quoted name in the flag's usage
545 // string; the first such item in the message is taken to be a parameter
546 // name to show in the message and the back quotes are stripped from
547 // the message when displayed. For instance, given
548 // flag.String("I", "", "search `directory` for include files")
549 // the output will be
551 // search directory for include files.
553 // To change the destination for flag messages, call CommandLine.SetOutput.
554 func PrintDefaults() {
555 CommandLine.PrintDefaults()
558 // defaultUsage is the default function to print a usage message.
559 func (f *FlagSet) defaultUsage() {
561 fmt.Fprintf(f.Output(), "Usage:\n")
563 fmt.Fprintf(f.Output(), "Usage of %s:\n", f.name)
568 // NOTE: Usage is not just defaultUsage(CommandLine)
569 // because it serves (via godoc flag Usage) as the example
570 // for how to write your own usage function.
572 // Usage prints a usage message documenting all defined command-line flags
573 // to CommandLine's output, which by default is os.Stderr.
574 // It is called when an error occurs while parsing flags.
575 // The function is a variable that may be changed to point to a custom function.
576 // By default it prints a simple header and calls PrintDefaults; for details about the
577 // format of the output and how to control it, see the documentation for PrintDefaults.
578 // Custom usage functions may choose to exit the program; by default exiting
579 // happens anyway as the command line's error handling strategy is set to
582 fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
586 // NFlag returns the number of flags that have been set.
587 func (f *FlagSet) NFlag() int { return len(f.actual) }
589 // NFlag returns the number of command-line flags that have been set.
590 func NFlag() int { return len(CommandLine.actual) }
592 // Arg returns the i'th argument. Arg(0) is the first remaining argument
593 // after flags have been processed. Arg returns an empty string if the
594 // requested element does not exist.
595 func (f *FlagSet) Arg(i int) string {
596 if i < 0 || i >= len(f.args) {
602 // Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
603 // after flags have been processed. Arg returns an empty string if the
604 // requested element does not exist.
605 func Arg(i int) string {
606 return CommandLine.Arg(i)
609 // NArg is the number of arguments remaining after flags have been processed.
610 func (f *FlagSet) NArg() int { return len(f.args) }
612 // NArg is the number of arguments remaining after flags have been processed.
613 func NArg() int { return len(CommandLine.args) }
615 // Args returns the non-flag arguments.
616 func (f *FlagSet) Args() []string { return f.args }
618 // Args returns the non-flag command-line arguments.
619 func Args() []string { return CommandLine.args }
621 // BoolVar defines a bool flag with specified name, default value, and usage string.
622 // The argument p points to a bool variable in which to store the value of the flag.
623 func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
624 f.Var(newBoolValue(value, p), name, usage)
627 // BoolVar defines a bool flag with specified name, default value, and usage string.
628 // The argument p points to a bool variable in which to store the value of the flag.
629 func BoolVar(p *bool, name string, value bool, usage string) {
630 CommandLine.Var(newBoolValue(value, p), name, usage)
633 // Bool defines a bool flag with specified name, default value, and usage string.
634 // The return value is the address of a bool variable that stores the value of the flag.
635 func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
637 f.BoolVar(p, name, value, usage)
641 // Bool defines a bool flag with specified name, default value, and usage string.
642 // The return value is the address of a bool variable that stores the value of the flag.
643 func Bool(name string, value bool, usage string) *bool {
644 return CommandLine.Bool(name, value, usage)
647 // IntVar defines an int flag with specified name, default value, and usage string.
648 // The argument p points to an int variable in which to store the value of the flag.
649 func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
650 f.Var(newIntValue(value, p), name, usage)
653 // IntVar defines an int flag with specified name, default value, and usage string.
654 // The argument p points to an int variable in which to store the value of the flag.
655 func IntVar(p *int, name string, value int, usage string) {
656 CommandLine.Var(newIntValue(value, p), name, usage)
659 // Int defines an int flag with specified name, default value, and usage string.
660 // The return value is the address of an int variable that stores the value of the flag.
661 func (f *FlagSet) Int(name string, value int, usage string) *int {
663 f.IntVar(p, name, value, usage)
667 // Int defines an int flag with specified name, default value, and usage string.
668 // The return value is the address of an int variable that stores the value of the flag.
669 func Int(name string, value int, usage string) *int {
670 return CommandLine.Int(name, value, usage)
673 // Int64Var defines an int64 flag with specified name, default value, and usage string.
674 // The argument p points to an int64 variable in which to store the value of the flag.
675 func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
676 f.Var(newInt64Value(value, p), name, usage)
679 // Int64Var defines an int64 flag with specified name, default value, and usage string.
680 // The argument p points to an int64 variable in which to store the value of the flag.
681 func Int64Var(p *int64, name string, value int64, usage string) {
682 CommandLine.Var(newInt64Value(value, p), name, usage)
685 // Int64 defines an int64 flag with specified name, default value, and usage string.
686 // The return value is the address of an int64 variable that stores the value of the flag.
687 func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
689 f.Int64Var(p, name, value, usage)
693 // Int64 defines an int64 flag with specified name, default value, and usage string.
694 // The return value is the address of an int64 variable that stores the value of the flag.
695 func Int64(name string, value int64, usage string) *int64 {
696 return CommandLine.Int64(name, value, usage)
699 // UintVar defines a uint flag with specified name, default value, and usage string.
700 // The argument p points to a uint variable in which to store the value of the flag.
701 func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
702 f.Var(newUintValue(value, p), name, usage)
705 // UintVar defines a uint flag with specified name, default value, and usage string.
706 // The argument p points to a uint variable in which to store the value of the flag.
707 func UintVar(p *uint, name string, value uint, usage string) {
708 CommandLine.Var(newUintValue(value, p), name, usage)
711 // Uint defines a uint flag with specified name, default value, and usage string.
712 // The return value is the address of a uint variable that stores the value of the flag.
713 func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
715 f.UintVar(p, name, value, usage)
719 // Uint defines a uint flag with specified name, default value, and usage string.
720 // The return value is the address of a uint variable that stores the value of the flag.
721 func Uint(name string, value uint, usage string) *uint {
722 return CommandLine.Uint(name, value, usage)
725 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
726 // The argument p points to a uint64 variable in which to store the value of the flag.
727 func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
728 f.Var(newUint64Value(value, p), name, usage)
731 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
732 // The argument p points to a uint64 variable in which to store the value of the flag.
733 func Uint64Var(p *uint64, name string, value uint64, usage string) {
734 CommandLine.Var(newUint64Value(value, p), name, usage)
737 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
738 // The return value is the address of a uint64 variable that stores the value of the flag.
739 func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
741 f.Uint64Var(p, name, value, usage)
745 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
746 // The return value is the address of a uint64 variable that stores the value of the flag.
747 func Uint64(name string, value uint64, usage string) *uint64 {
748 return CommandLine.Uint64(name, value, usage)
751 // StringVar defines a string flag with specified name, default value, and usage string.
752 // The argument p points to a string variable in which to store the value of the flag.
753 func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
754 f.Var(newStringValue(value, p), name, usage)
757 // StringVar defines a string flag with specified name, default value, and usage string.
758 // The argument p points to a string variable in which to store the value of the flag.
759 func StringVar(p *string, name string, value string, usage string) {
760 CommandLine.Var(newStringValue(value, p), name, usage)
763 // String defines a string flag with specified name, default value, and usage string.
764 // The return value is the address of a string variable that stores the value of the flag.
765 func (f *FlagSet) String(name string, value string, usage string) *string {
767 f.StringVar(p, name, value, usage)
771 // String defines a string flag with specified name, default value, and usage string.
772 // The return value is the address of a string variable that stores the value of the flag.
773 func String(name string, value string, usage string) *string {
774 return CommandLine.String(name, value, usage)
777 // Float64Var defines a float64 flag with specified name, default value, and usage string.
778 // The argument p points to a float64 variable in which to store the value of the flag.
779 func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
780 f.Var(newFloat64Value(value, p), name, usage)
783 // Float64Var defines a float64 flag with specified name, default value, and usage string.
784 // The argument p points to a float64 variable in which to store the value of the flag.
785 func Float64Var(p *float64, name string, value float64, usage string) {
786 CommandLine.Var(newFloat64Value(value, p), name, usage)
789 // Float64 defines a float64 flag with specified name, default value, and usage string.
790 // The return value is the address of a float64 variable that stores the value of the flag.
791 func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
793 f.Float64Var(p, name, value, usage)
797 // Float64 defines a float64 flag with specified name, default value, and usage string.
798 // The return value is the address of a float64 variable that stores the value of the flag.
799 func Float64(name string, value float64, usage string) *float64 {
800 return CommandLine.Float64(name, value, usage)
803 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
804 // The argument p points to a time.Duration variable in which to store the value of the flag.
805 // The flag accepts a value acceptable to time.ParseDuration.
806 func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
807 f.Var(newDurationValue(value, p), name, usage)
810 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
811 // The argument p points to a time.Duration variable in which to store the value of the flag.
812 // The flag accepts a value acceptable to time.ParseDuration.
813 func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
814 CommandLine.Var(newDurationValue(value, p), name, usage)
817 // Duration defines a time.Duration flag with specified name, default value, and usage string.
818 // The return value is the address of a time.Duration variable that stores the value of the flag.
819 // The flag accepts a value acceptable to time.ParseDuration.
820 func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
821 p := new(time.Duration)
822 f.DurationVar(p, name, value, usage)
826 // Duration defines a time.Duration flag with specified name, default value, and usage string.
827 // The return value is the address of a time.Duration variable that stores the value of the flag.
828 // The flag accepts a value acceptable to time.ParseDuration.
829 func Duration(name string, value time.Duration, usage string) *time.Duration {
830 return CommandLine.Duration(name, value, usage)
833 // Var defines a flag with the specified name and usage string. The type and
834 // value of the flag are represented by the first argument, of type Value, which
835 // typically holds a user-defined implementation of Value. For instance, the
836 // caller could create a flag that turns a comma-separated string into a slice
837 // of strings by giving the slice the methods of Value; in particular, Set would
838 // decompose the comma-separated string into the slice.
839 func (f *FlagSet) Var(value Value, name string, usage string) {
840 // Remember the default value as a string; it won't change.
841 flag := &Flag{name, usage, value, value.String()}
842 _, alreadythere := f.formal[name]
846 msg = fmt.Sprintf("flag redefined: %s", name)
848 msg = fmt.Sprintf("%s flag redefined: %s", f.name, name)
850 fmt.Fprintln(f.Output(), msg)
851 panic(msg) // Happens only if flags are declared with identical names
854 f.formal = make(map[string]*Flag)
856 f.formal[name] = flag
859 // Var defines a flag with the specified name and usage string. The type and
860 // value of the flag are represented by the first argument, of type Value, which
861 // typically holds a user-defined implementation of Value. For instance, the
862 // caller could create a flag that turns a comma-separated string into a slice
863 // of strings by giving the slice the methods of Value; in particular, Set would
864 // decompose the comma-separated string into the slice.
865 func Var(value Value, name string, usage string) {
866 CommandLine.Var(value, name, usage)
869 // failf prints to standard error a formatted error and usage message and
870 // returns the error.
871 func (f *FlagSet) failf(format string, a ...interface{}) error {
872 err := fmt.Errorf(format, a...)
873 fmt.Fprintln(f.Output(), err)
878 // usage calls the Usage method for the flag set if one is specified,
879 // or the appropriate default usage function otherwise.
880 func (f *FlagSet) usage() {
888 // parseOne parses one flag. It reports whether a flag was seen.
889 func (f *FlagSet) parseOne() (bool, error) {
890 if len(f.args) == 0 {
894 if len(s) < 2 || s[0] != '-' {
900 if len(s) == 2 { // "--" terminates the flags
905 name := s[numMinuses:]
906 if len(name) == 0 || name[0] == '-' || name[0] == '=' {
907 return false, f.failf("bad flag syntax: %s", s)
910 // it's a flag. does it have an argument?
914 for i := 1; i < len(name); i++ { // equals cannot be first
923 flag, alreadythere := m[name] // BUG
925 if name == "help" || name == "h" { // special case for nice help message.
927 return false, ErrHelp
929 return false, f.failf("flag provided but not defined: -%s", name)
932 if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
934 if err := fv.Set(value); err != nil {
935 return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err)
938 if err := fv.Set("true"); err != nil {
939 return false, f.failf("invalid boolean flag %s: %v", name, err)
943 // It must have a value, which might be the next argument.
944 if !hasValue && len(f.args) > 0 {
945 // value is the next arg
947 value, f.args = f.args[0], f.args[1:]
950 return false, f.failf("flag needs an argument: -%s", name)
952 if err := flag.Value.Set(value); err != nil {
953 return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
957 f.actual = make(map[string]*Flag)
959 f.actual[name] = flag
963 // Parse parses flag definitions from the argument list, which should not
964 // include the command name. Must be called after all flags in the FlagSet
965 // are defined and before flags are accessed by the program.
966 // The return value will be ErrHelp if -help or -h were set but not defined.
967 func (f *FlagSet) Parse(arguments []string) error {
971 seen, err := f.parseOne()
978 switch f.errorHandling {
979 case ContinueOnError:
990 // Parsed reports whether f.Parse has been called.
991 func (f *FlagSet) Parsed() bool {
995 // Parse parses the command-line flags from os.Args[1:]. Must be called
996 // after all flags are defined and before flags are accessed by the program.
998 // Ignore errors; CommandLine is set for ExitOnError.
999 CommandLine.Parse(os.Args[1:])
1002 // Parsed reports whether the command-line flags have been parsed.
1003 func Parsed() bool {
1004 return CommandLine.Parsed()
1007 // CommandLine is the default set of command-line flags, parsed from os.Args.
1008 // The top-level functions such as BoolVar, Arg, and so on are wrappers for the
1009 // methods of CommandLine.
1010 var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
1013 // Override generic FlagSet default Usage with call to global Usage.
1014 // Note: This is not CommandLine.Usage = Usage,
1015 // because we want any eventual call to use any updated value of Usage,
1016 // not the value it has when this line is run.
1017 CommandLine.Usage = commandLineUsage
1020 func commandLineUsage() {
1024 // NewFlagSet returns a new, empty flag set with the specified name and
1025 // error handling property. If the name is not empty, it will be printed
1026 // in the default usage message and in error messages.
1027 func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
1030 errorHandling: errorHandling,
1032 f.Usage = f.defaultUsage
1036 // Init sets the name and error handling property for a flag set.
1037 // By default, the zero FlagSet uses an empty name and the
1038 // ContinueOnError error handling policy.
1039 func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
1041 f.errorHandling = errorHandling