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, -n, stored in the pointer nFlag, with type *int:
15 var nFlag = flag.Int("n", 1234, "help message for flag n")
17 If you like, you can bind the flag to a variable using the Var() functions.
21 flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
24 Or you can create custom flags that satisfy the Value interface (with
25 pointer receivers) and couple them to flag parsing by
27 flag.Var(&flagVal, "name", "help message for flagname")
29 For such flags, the default value is just the initial value of the variable.
31 After all flags are defined, call
35 to parse the command line into the defined flags.
37 Flags may then be used directly. If you're using the flags themselves,
38 they are all pointers; if you bind to variables, they're values.
40 fmt.Println("ip has value ", *ip)
41 fmt.Println("flagvar has value ", flagvar)
43 After parsing, the arguments following the flags are available as the
44 slice flag.Args() or individually as flag.Arg(i).
45 The arguments are indexed from 0 through flag.NArg()-1.
47 # Command line flag syntax
49 The following forms are permitted:
52 --flag // double dashes are also permitted
54 -flag x // non-boolean flags only
56 One or two dashes may be used; they are equivalent.
57 The last form is not permitted for boolean flags because the
58 meaning of the command
62 where * is a Unix shell wildcard, will change if there is a file
63 called 0, false, etc. You must use the -flag=false form to turn
66 Flag parsing stops just before the first non-flag argument
67 ("-" is a non-flag argument) or after the terminator "--".
69 Integer flags accept 1234, 0664, 0x1234 and may be negative.
72 1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
74 Duration flags accept any input valid for time.ParseDuration.
76 The default set of command-line flags is controlled by
77 top-level functions. The FlagSet type allows one to define
78 independent sets of flags, such as to implement subcommands
79 in a command-line interface. The methods of FlagSet are
80 analogous to the top-level functions for the command-line
98 // ErrHelp is the error returned if the -help or -h flag is invoked
99 // but no such flag is defined.
100 var ErrHelp = errors.New("flag: help requested")
102 // errParse is returned by Set if a flag's value fails to parse, such as with an invalid integer for Int.
103 // It then gets wrapped through failf to provide more information.
104 var errParse = errors.New("parse error")
106 // errRange is returned by Set if a flag's value is out of range.
107 // It then gets wrapped through failf to provide more information.
108 var errRange = errors.New("value out of range")
110 func numError(err error) error {
111 ne, ok := err.(*strconv.NumError)
115 if ne.Err == strconv.ErrSyntax {
118 if ne.Err == strconv.ErrRange {
127 func newBoolValue(val bool, p *bool) *boolValue {
129 return (*boolValue)(p)
132 func (b *boolValue) Set(s string) error {
133 v, err := strconv.ParseBool(s)
141 func (b *boolValue) Get() any { return bool(*b) }
143 func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
145 func (b *boolValue) IsBoolFlag() bool { return true }
147 // optional interface to indicate boolean flags that can be
148 // supplied without "=value" text
149 type boolFlag interface {
157 func newIntValue(val int, p *int) *intValue {
159 return (*intValue)(p)
162 func (i *intValue) Set(s string) error {
163 v, err := strconv.ParseInt(s, 0, strconv.IntSize)
171 func (i *intValue) Get() any { return int(*i) }
173 func (i *intValue) String() string { return strconv.Itoa(int(*i)) }
176 type int64Value int64
178 func newInt64Value(val int64, p *int64) *int64Value {
180 return (*int64Value)(p)
183 func (i *int64Value) Set(s string) error {
184 v, err := strconv.ParseInt(s, 0, 64)
192 func (i *int64Value) Get() any { return int64(*i) }
194 func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) }
199 func newUintValue(val uint, p *uint) *uintValue {
201 return (*uintValue)(p)
204 func (i *uintValue) Set(s string) error {
205 v, err := strconv.ParseUint(s, 0, strconv.IntSize)
213 func (i *uintValue) Get() any { return uint(*i) }
215 func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) }
218 type uint64Value uint64
220 func newUint64Value(val uint64, p *uint64) *uint64Value {
222 return (*uint64Value)(p)
225 func (i *uint64Value) Set(s string) error {
226 v, err := strconv.ParseUint(s, 0, 64)
234 func (i *uint64Value) Get() any { return uint64(*i) }
236 func (i *uint64Value) String() string { return strconv.FormatUint(uint64(*i), 10) }
239 type stringValue string
241 func newStringValue(val string, p *string) *stringValue {
243 return (*stringValue)(p)
246 func (s *stringValue) Set(val string) error {
247 *s = stringValue(val)
251 func (s *stringValue) Get() any { return string(*s) }
253 func (s *stringValue) String() string { return string(*s) }
256 type float64Value float64
258 func newFloat64Value(val float64, p *float64) *float64Value {
260 return (*float64Value)(p)
263 func (f *float64Value) Set(s string) error {
264 v, err := strconv.ParseFloat(s, 64)
272 func (f *float64Value) Get() any { return float64(*f) }
274 func (f *float64Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }
276 // -- time.Duration Value
277 type durationValue time.Duration
279 func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
281 return (*durationValue)(p)
284 func (d *durationValue) Set(s string) error {
285 v, err := time.ParseDuration(s)
289 *d = durationValue(v)
293 func (d *durationValue) Get() any { return time.Duration(*d) }
295 func (d *durationValue) String() string { return (*time.Duration)(d).String() }
297 // -- encoding.TextUnmarshaler Value
298 type textValue struct{ p encoding.TextUnmarshaler }
300 func newTextValue(val encoding.TextMarshaler, p encoding.TextUnmarshaler) textValue {
301 ptrVal := reflect.ValueOf(p)
302 if ptrVal.Kind() != reflect.Ptr {
303 panic("variable value type must be a pointer")
305 defVal := reflect.ValueOf(val)
306 if defVal.Kind() == reflect.Ptr {
307 defVal = defVal.Elem()
309 if defVal.Type() != ptrVal.Type().Elem() {
310 panic(fmt.Sprintf("default type does not match variable type: %v != %v", defVal.Type(), ptrVal.Type().Elem()))
312 ptrVal.Elem().Set(defVal)
316 func (v textValue) Set(s string) error {
317 return v.p.UnmarshalText([]byte(s))
320 func (v textValue) Get() interface{} {
324 func (v textValue) String() string {
325 if m, ok := v.p.(encoding.TextMarshaler); ok {
326 if b, err := m.MarshalText(); err == nil {
334 type funcValue func(string) error
336 func (f funcValue) Set(s string) error { return f(s) }
338 func (f funcValue) String() string { return "" }
340 // Value is the interface to the dynamic value stored in a flag.
341 // (The default value is represented as a string.)
343 // If a Value has an IsBoolFlag() bool method returning true,
344 // the command-line parser makes -name equivalent to -name=true
345 // rather than using the next command-line argument.
347 // Set is called once, in command line order, for each flag present.
348 // The flag package may call the String method with a zero-valued receiver,
349 // such as a nil pointer.
350 type Value interface {
355 // Getter is an interface that allows the contents of a Value to be retrieved.
356 // It wraps the Value interface, rather than being part of it, because it
357 // appeared after Go 1 and its compatibility rules. All Value types provided
358 // by this package satisfy the Getter interface, except the type used by Func.
359 type Getter interface {
364 // ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
365 type ErrorHandling int
367 // These constants cause FlagSet.Parse to behave as described if the parse fails.
369 ContinueOnError ErrorHandling = iota // Return a descriptive error.
370 ExitOnError // Call os.Exit(2) or for -h/-help Exit(0).
371 PanicOnError // Call panic with a descriptive error.
374 // A FlagSet represents a set of defined flags. The zero value of a FlagSet
375 // has no name and has ContinueOnError error handling.
377 // Flag names must be unique within a FlagSet. An attempt to define a flag whose
378 // name is already in use will cause a panic.
379 type FlagSet struct {
380 // Usage is the function called when an error occurs while parsing flags.
381 // The field is a function (not a method) that may be changed to point to
382 // a custom error handler. What happens after Usage is called depends
383 // on the ErrorHandling setting; for the command line, this defaults
384 // to ExitOnError, which exits the program after calling Usage.
389 actual map[string]*Flag
390 formal map[string]*Flag
391 args []string // arguments after flags
392 errorHandling ErrorHandling
393 output io.Writer // nil means stderr; use Output() accessor
396 // A Flag represents the state of a flag.
398 Name string // name as it appears on command line
399 Usage string // help message
400 Value Value // value as set
401 DefValue string // default value (as text); for usage message
404 // sortFlags returns the flags as a slice in lexicographical sorted order.
405 func sortFlags(flags map[string]*Flag) []*Flag {
406 result := make([]*Flag, len(flags))
408 for _, f := range flags {
412 sort.Slice(result, func(i, j int) bool {
413 return result[i].Name < result[j].Name
418 // Output returns the destination for usage and error messages. os.Stderr is returned if
419 // output was not set or was set to nil.
420 func (f *FlagSet) Output() io.Writer {
427 // Name returns the name of the flag set.
428 func (f *FlagSet) Name() string {
432 // ErrorHandling returns the error handling behavior of the flag set.
433 func (f *FlagSet) ErrorHandling() ErrorHandling {
434 return f.errorHandling
437 // SetOutput sets the destination for usage and error messages.
438 // If output is nil, os.Stderr is used.
439 func (f *FlagSet) SetOutput(output io.Writer) {
443 // VisitAll visits the flags in lexicographical order, calling fn for each.
444 // It visits all flags, even those not set.
445 func (f *FlagSet) VisitAll(fn func(*Flag)) {
446 for _, flag := range sortFlags(f.formal) {
451 // VisitAll visits the command-line flags in lexicographical order, calling
452 // fn for each. It visits all flags, even those not set.
453 func VisitAll(fn func(*Flag)) {
454 CommandLine.VisitAll(fn)
457 // Visit visits the flags in lexicographical order, calling fn for each.
458 // It visits only those flags that have been set.
459 func (f *FlagSet) Visit(fn func(*Flag)) {
460 for _, flag := range sortFlags(f.actual) {
465 // Visit visits the command-line flags in lexicographical order, calling fn
466 // for each. It visits only those flags that have been set.
467 func Visit(fn func(*Flag)) {
468 CommandLine.Visit(fn)
471 // Lookup returns the Flag structure of the named flag, returning nil if none exists.
472 func (f *FlagSet) Lookup(name string) *Flag {
473 return f.formal[name]
476 // Lookup returns the Flag structure of the named command-line flag,
477 // returning nil if none exists.
478 func Lookup(name string) *Flag {
479 return CommandLine.formal[name]
482 // Set sets the value of the named flag.
483 func (f *FlagSet) Set(name, value string) error {
484 flag, ok := f.formal[name]
486 return fmt.Errorf("no such flag -%v", name)
488 err := flag.Value.Set(value)
493 f.actual = make(map[string]*Flag)
495 f.actual[name] = flag
499 // Set sets the value of the named command-line flag.
500 func Set(name, value string) error {
501 return CommandLine.Set(name, value)
504 // isZeroValue determines whether the string represents the zero
506 func isZeroValue(flag *Flag, value string) (ok bool, err error) {
507 // Build a zero value of the flag's Value type, and see if the
508 // result of calling its String method equals the value passed in.
509 // This works unless the Value type is itself an interface type.
510 typ := reflect.TypeOf(flag.Value)
512 if typ.Kind() == reflect.Pointer {
513 z = reflect.New(typ.Elem())
515 z = reflect.Zero(typ)
517 // Catch panics calling the String method, which shouldn't prevent the
518 // usage message from being printed, but that we should report to the
519 // user so that they know to fix their code.
521 if e := recover(); e != nil {
522 if typ.Kind() == reflect.Pointer {
525 err = fmt.Errorf("panic calling String method on zero %v for flag %s: %v", typ, flag.Name, e)
528 return value == z.Interface().(Value).String(), nil
531 // UnquoteUsage extracts a back-quoted name from the usage
532 // string for a flag and returns it and the un-quoted usage.
533 // Given "a `name` to show" it returns ("name", "a name to show").
534 // If there are no back quotes, the name is an educated guess of the
535 // type of the flag's value, or the empty string if the flag is boolean.
536 func UnquoteUsage(flag *Flag) (name string, usage string) {
537 // Look for a back-quoted name, but avoid the strings package.
539 for i := 0; i < len(usage); i++ {
541 for j := i + 1; j < len(usage); j++ {
543 name = usage[i+1 : j]
544 usage = usage[:i] + name + usage[j+1:]
548 break // Only one back quote; use type name.
551 // No explicit name, so use type if we can find one.
553 switch fv := flag.Value.(type) {
562 case *intValue, *int64Value:
566 case *uintValue, *uint64Value:
572 // PrintDefaults prints, to standard error unless configured otherwise, the
573 // default values of all defined command-line flags in the set. See the
574 // documentation for the global function PrintDefaults for more information.
575 func (f *FlagSet) PrintDefaults() {
576 var isZeroValueErrs []error
577 f.VisitAll(func(flag *Flag) {
578 var b strings.Builder
579 fmt.Fprintf(&b, " -%s", flag.Name) // Two spaces before -; see next two comments.
580 name, usage := UnquoteUsage(flag)
585 // Boolean flags of one ASCII letter are so common we
586 // treat them specially, putting their usage on the same line.
587 if b.Len() <= 4 { // space, space, '-', 'x'.
590 // Four spaces before the tab triggers good alignment
591 // for both 4- and 8-space tab stops.
592 b.WriteString("\n \t")
594 b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
596 // Print the default value only if it differs to the zero value
597 // for this flag type.
598 if isZero, err := isZeroValue(flag, flag.DefValue); err != nil {
599 isZeroValueErrs = append(isZeroValueErrs, err)
601 if _, ok := flag.Value.(*stringValue); ok {
602 // put quotes on the value
603 fmt.Fprintf(&b, " (default %q)", flag.DefValue)
605 fmt.Fprintf(&b, " (default %v)", flag.DefValue)
608 fmt.Fprint(f.Output(), b.String(), "\n")
610 // If calling String on any zero flag.Values triggered a panic, print
611 // the messages after the full set of defaults so that the programmer
612 // knows to fix the panic.
613 if errs := isZeroValueErrs; len(errs) > 0 {
614 fmt.Fprintln(f.Output())
615 for _, err := range errs {
616 fmt.Fprintln(f.Output(), err)
621 // PrintDefaults prints, to standard error unless configured otherwise,
622 // a usage message showing the default settings of all defined
623 // command-line flags.
624 // For an integer valued flag x, the default output has the form
627 // usage-message-for-x (default 7)
629 // The usage message will appear on a separate line for anything but
630 // a bool flag with a one-byte name. For bool flags, the type is
631 // omitted and if the flag name is one byte the usage message appears
632 // on the same line. The parenthetical default is omitted if the
633 // default is the zero value for the type. The listed type, here int,
634 // can be changed by placing a back-quoted name in the flag's usage
635 // string; the first such item in the message is taken to be a parameter
636 // name to show in the message and the back quotes are stripped from
637 // the message when displayed. For instance, given
639 // flag.String("I", "", "search `directory` for include files")
641 // the output will be
644 // search directory for include files.
646 // To change the destination for flag messages, call CommandLine.SetOutput.
647 func PrintDefaults() {
648 CommandLine.PrintDefaults()
651 // defaultUsage is the default function to print a usage message.
652 func (f *FlagSet) defaultUsage() {
654 fmt.Fprintf(f.Output(), "Usage:\n")
656 fmt.Fprintf(f.Output(), "Usage of %s:\n", f.name)
661 // NOTE: Usage is not just defaultUsage(CommandLine)
662 // because it serves (via godoc flag Usage) as the example
663 // for how to write your own usage function.
665 // Usage prints a usage message documenting all defined command-line flags
666 // to CommandLine's output, which by default is os.Stderr.
667 // It is called when an error occurs while parsing flags.
668 // The function is a variable that may be changed to point to a custom function.
669 // By default it prints a simple header and calls PrintDefaults; for details about the
670 // format of the output and how to control it, see the documentation for PrintDefaults.
671 // Custom usage functions may choose to exit the program; by default exiting
672 // happens anyway as the command line's error handling strategy is set to
675 fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
679 // NFlag returns the number of flags that have been set.
680 func (f *FlagSet) NFlag() int { return len(f.actual) }
682 // NFlag returns the number of command-line flags that have been set.
683 func NFlag() int { return len(CommandLine.actual) }
685 // Arg returns the i'th argument. Arg(0) is the first remaining argument
686 // after flags have been processed. Arg returns an empty string if the
687 // requested element does not exist.
688 func (f *FlagSet) Arg(i int) string {
689 if i < 0 || i >= len(f.args) {
695 // Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
696 // after flags have been processed. Arg returns an empty string if the
697 // requested element does not exist.
698 func Arg(i int) string {
699 return CommandLine.Arg(i)
702 // NArg is the number of arguments remaining after flags have been processed.
703 func (f *FlagSet) NArg() int { return len(f.args) }
705 // NArg is the number of arguments remaining after flags have been processed.
706 func NArg() int { return len(CommandLine.args) }
708 // Args returns the non-flag arguments.
709 func (f *FlagSet) Args() []string { return f.args }
711 // Args returns the non-flag command-line arguments.
712 func Args() []string { return CommandLine.args }
714 // BoolVar defines a bool flag with specified name, default value, and usage string.
715 // The argument p points to a bool variable in which to store the value of the flag.
716 func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
717 f.Var(newBoolValue(value, p), name, usage)
720 // BoolVar defines a bool flag with specified name, default value, and usage string.
721 // The argument p points to a bool variable in which to store the value of the flag.
722 func BoolVar(p *bool, name string, value bool, usage string) {
723 CommandLine.Var(newBoolValue(value, p), name, usage)
726 // Bool defines a bool flag with specified name, default value, and usage string.
727 // The return value is the address of a bool variable that stores the value of the flag.
728 func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
730 f.BoolVar(p, name, value, usage)
734 // Bool defines a bool flag with specified name, default value, and usage string.
735 // The return value is the address of a bool variable that stores the value of the flag.
736 func Bool(name string, value bool, usage string) *bool {
737 return CommandLine.Bool(name, value, usage)
740 // IntVar defines an int flag with specified name, default value, and usage string.
741 // The argument p points to an int variable in which to store the value of the flag.
742 func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
743 f.Var(newIntValue(value, p), name, usage)
746 // IntVar defines an int flag with specified name, default value, and usage string.
747 // The argument p points to an int variable in which to store the value of the flag.
748 func IntVar(p *int, name string, value int, usage string) {
749 CommandLine.Var(newIntValue(value, p), name, usage)
752 // Int defines an int flag with specified name, default value, and usage string.
753 // The return value is the address of an int variable that stores the value of the flag.
754 func (f *FlagSet) Int(name string, value int, usage string) *int {
756 f.IntVar(p, name, value, usage)
760 // Int defines an int flag with specified name, default value, and usage string.
761 // The return value is the address of an int variable that stores the value of the flag.
762 func Int(name string, value int, usage string) *int {
763 return CommandLine.Int(name, value, usage)
766 // Int64Var defines an int64 flag with specified name, default value, and usage string.
767 // The argument p points to an int64 variable in which to store the value of the flag.
768 func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
769 f.Var(newInt64Value(value, p), name, usage)
772 // Int64Var defines an int64 flag with specified name, default value, and usage string.
773 // The argument p points to an int64 variable in which to store the value of the flag.
774 func Int64Var(p *int64, name string, value int64, usage string) {
775 CommandLine.Var(newInt64Value(value, p), name, usage)
778 // Int64 defines an int64 flag with specified name, default value, and usage string.
779 // The return value is the address of an int64 variable that stores the value of the flag.
780 func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
782 f.Int64Var(p, name, value, usage)
786 // Int64 defines an int64 flag with specified name, default value, and usage string.
787 // The return value is the address of an int64 variable that stores the value of the flag.
788 func Int64(name string, value int64, usage string) *int64 {
789 return CommandLine.Int64(name, value, usage)
792 // UintVar defines a uint flag with specified name, default value, and usage string.
793 // The argument p points to a uint variable in which to store the value of the flag.
794 func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
795 f.Var(newUintValue(value, p), name, usage)
798 // UintVar defines a uint flag with specified name, default value, and usage string.
799 // The argument p points to a uint variable in which to store the value of the flag.
800 func UintVar(p *uint, name string, value uint, usage string) {
801 CommandLine.Var(newUintValue(value, p), name, usage)
804 // Uint defines a uint flag with specified name, default value, and usage string.
805 // The return value is the address of a uint variable that stores the value of the flag.
806 func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
808 f.UintVar(p, name, value, usage)
812 // Uint defines a uint flag with specified name, default value, and usage string.
813 // The return value is the address of a uint variable that stores the value of the flag.
814 func Uint(name string, value uint, usage string) *uint {
815 return CommandLine.Uint(name, value, usage)
818 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
819 // The argument p points to a uint64 variable in which to store the value of the flag.
820 func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
821 f.Var(newUint64Value(value, p), name, usage)
824 // Uint64Var defines a uint64 flag with specified name, default value, and usage string.
825 // The argument p points to a uint64 variable in which to store the value of the flag.
826 func Uint64Var(p *uint64, name string, value uint64, usage string) {
827 CommandLine.Var(newUint64Value(value, p), name, usage)
830 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
831 // The return value is the address of a uint64 variable that stores the value of the flag.
832 func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
834 f.Uint64Var(p, name, value, usage)
838 // Uint64 defines a uint64 flag with specified name, default value, and usage string.
839 // The return value is the address of a uint64 variable that stores the value of the flag.
840 func Uint64(name string, value uint64, usage string) *uint64 {
841 return CommandLine.Uint64(name, value, usage)
844 // StringVar defines a string flag with specified name, default value, and usage string.
845 // The argument p points to a string variable in which to store the value of the flag.
846 func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
847 f.Var(newStringValue(value, p), name, usage)
850 // StringVar defines a string flag with specified name, default value, and usage string.
851 // The argument p points to a string variable in which to store the value of the flag.
852 func StringVar(p *string, name string, value string, usage string) {
853 CommandLine.Var(newStringValue(value, p), name, usage)
856 // String defines a string flag with specified name, default value, and usage string.
857 // The return value is the address of a string variable that stores the value of the flag.
858 func (f *FlagSet) String(name string, value string, usage string) *string {
860 f.StringVar(p, name, value, usage)
864 // String defines a string flag with specified name, default value, and usage string.
865 // The return value is the address of a string variable that stores the value of the flag.
866 func String(name string, value string, usage string) *string {
867 return CommandLine.String(name, value, usage)
870 // Float64Var defines a float64 flag with specified name, default value, and usage string.
871 // The argument p points to a float64 variable in which to store the value of the flag.
872 func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
873 f.Var(newFloat64Value(value, p), name, usage)
876 // Float64Var defines a float64 flag with specified name, default value, and usage string.
877 // The argument p points to a float64 variable in which to store the value of the flag.
878 func Float64Var(p *float64, name string, value float64, usage string) {
879 CommandLine.Var(newFloat64Value(value, p), name, usage)
882 // Float64 defines a float64 flag with specified name, default value, and usage string.
883 // The return value is the address of a float64 variable that stores the value of the flag.
884 func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
886 f.Float64Var(p, name, value, usage)
890 // Float64 defines a float64 flag with specified name, default value, and usage string.
891 // The return value is the address of a float64 variable that stores the value of the flag.
892 func Float64(name string, value float64, usage string) *float64 {
893 return CommandLine.Float64(name, value, usage)
896 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
897 // The argument p points to a time.Duration variable in which to store the value of the flag.
898 // The flag accepts a value acceptable to time.ParseDuration.
899 func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
900 f.Var(newDurationValue(value, p), name, usage)
903 // DurationVar defines a time.Duration flag with specified name, default value, and usage string.
904 // The argument p points to a time.Duration variable in which to store the value of the flag.
905 // The flag accepts a value acceptable to time.ParseDuration.
906 func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
907 CommandLine.Var(newDurationValue(value, p), name, usage)
910 // Duration defines a time.Duration flag with specified name, default value, and usage string.
911 // The return value is the address of a time.Duration variable that stores the value of the flag.
912 // The flag accepts a value acceptable to time.ParseDuration.
913 func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
914 p := new(time.Duration)
915 f.DurationVar(p, name, value, usage)
919 // Duration defines a time.Duration flag with specified name, default value, and usage string.
920 // The return value is the address of a time.Duration variable that stores the value of the flag.
921 // The flag accepts a value acceptable to time.ParseDuration.
922 func Duration(name string, value time.Duration, usage string) *time.Duration {
923 return CommandLine.Duration(name, value, usage)
926 // TextVar defines a flag with a specified name, default value, and usage string.
927 // The argument p must be a pointer to a variable that will hold the value
928 // of the flag, and p must implement encoding.TextUnmarshaler.
929 // If the flag is used, the flag value will be passed to p's UnmarshalText method.
930 // The type of the default value must be the same as the type of p.
931 func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) {
932 f.Var(newTextValue(value, p), name, usage)
935 // TextVar defines a flag with a specified name, default value, and usage string.
936 // The argument p must be a pointer to a variable that will hold the value
937 // of the flag, and p must implement encoding.TextUnmarshaler.
938 // If the flag is used, the flag value will be passed to p's UnmarshalText method.
939 // The type of the default value must be the same as the type of p.
940 func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) {
941 CommandLine.Var(newTextValue(value, p), name, usage)
944 // Func defines a flag with the specified name and usage string.
945 // Each time the flag is seen, fn is called with the value of the flag.
946 // If fn returns a non-nil error, it will be treated as a flag value parsing error.
947 func (f *FlagSet) Func(name, usage string, fn func(string) error) {
948 f.Var(funcValue(fn), name, usage)
951 // Func defines a flag with the specified name and usage string.
952 // Each time the flag is seen, fn is called with the value of the flag.
953 // If fn returns a non-nil error, it will be treated as a flag value parsing error.
954 func Func(name, usage string, fn func(string) error) {
955 CommandLine.Func(name, usage, fn)
958 // Var defines a flag with the specified name and usage string. The type and
959 // value of the flag are represented by the first argument, of type Value, which
960 // typically holds a user-defined implementation of Value. For instance, the
961 // caller could create a flag that turns a comma-separated string into a slice
962 // of strings by giving the slice the methods of Value; in particular, Set would
963 // decompose the comma-separated string into the slice.
964 func (f *FlagSet) Var(value Value, name string, usage string) {
965 // Flag must not begin "-" or contain "=".
966 if strings.HasPrefix(name, "-") {
967 panic(f.sprintf("flag %q begins with -", name))
968 } else if strings.Contains(name, "=") {
969 panic(f.sprintf("flag %q contains =", name))
972 // Remember the default value as a string; it won't change.
973 flag := &Flag{name, usage, value, value.String()}
974 _, alreadythere := f.formal[name]
978 msg = f.sprintf("flag redefined: %s", name)
980 msg = f.sprintf("%s flag redefined: %s", f.name, name)
982 panic(msg) // Happens only if flags are declared with identical names
985 f.formal = make(map[string]*Flag)
987 f.formal[name] = flag
990 // Var defines a flag with the specified name and usage string. The type and
991 // value of the flag are represented by the first argument, of type Value, which
992 // typically holds a user-defined implementation of Value. For instance, the
993 // caller could create a flag that turns a comma-separated string into a slice
994 // of strings by giving the slice the methods of Value; in particular, Set would
995 // decompose the comma-separated string into the slice.
996 func Var(value Value, name string, usage string) {
997 CommandLine.Var(value, name, usage)
1000 // sprintf formats the message, prints it to output, and returns it.
1001 func (f *FlagSet) sprintf(format string, a ...any) string {
1002 msg := fmt.Sprintf(format, a...)
1003 fmt.Fprintln(f.Output(), msg)
1007 // failf prints to standard error a formatted error and usage message and
1008 // returns the error.
1009 func (f *FlagSet) failf(format string, a ...any) error {
1010 msg := f.sprintf(format, a...)
1012 return errors.New(msg)
1015 // usage calls the Usage method for the flag set if one is specified,
1016 // or the appropriate default usage function otherwise.
1017 func (f *FlagSet) usage() {
1025 // parseOne parses one flag. It reports whether a flag was seen.
1026 func (f *FlagSet) parseOne() (bool, error) {
1027 if len(f.args) == 0 {
1031 if len(s) < 2 || s[0] != '-' {
1037 if len(s) == 2 { // "--" terminates the flags
1042 name := s[numMinuses:]
1043 if len(name) == 0 || name[0] == '-' || name[0] == '=' {
1044 return false, f.failf("bad flag syntax: %s", s)
1047 // it's a flag. does it have an argument?
1051 for i := 1; i < len(name); i++ { // equals cannot be first
1060 flag, ok := f.formal[name]
1062 if name == "help" || name == "h" { // special case for nice help message.
1064 return false, ErrHelp
1066 return false, f.failf("flag provided but not defined: -%s", name)
1069 if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
1071 if err := fv.Set(value); err != nil {
1072 return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err)
1075 if err := fv.Set("true"); err != nil {
1076 return false, f.failf("invalid boolean flag %s: %v", name, err)
1080 // It must have a value, which might be the next argument.
1081 if !hasValue && len(f.args) > 0 {
1082 // value is the next arg
1084 value, f.args = f.args[0], f.args[1:]
1087 return false, f.failf("flag needs an argument: -%s", name)
1089 if err := flag.Value.Set(value); err != nil {
1090 return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
1093 if f.actual == nil {
1094 f.actual = make(map[string]*Flag)
1096 f.actual[name] = flag
1100 // Parse parses flag definitions from the argument list, which should not
1101 // include the command name. Must be called after all flags in the FlagSet
1102 // are defined and before flags are accessed by the program.
1103 // The return value will be ErrHelp if -help or -h were set but not defined.
1104 func (f *FlagSet) Parse(arguments []string) error {
1108 seen, err := f.parseOne()
1115 switch f.errorHandling {
1116 case ContinueOnError:
1130 // Parsed reports whether f.Parse has been called.
1131 func (f *FlagSet) Parsed() bool {
1135 // Parse parses the command-line flags from os.Args[1:]. Must be called
1136 // after all flags are defined and before flags are accessed by the program.
1138 // Ignore errors; CommandLine is set for ExitOnError.
1139 CommandLine.Parse(os.Args[1:])
1142 // Parsed reports whether the command-line flags have been parsed.
1143 func Parsed() bool {
1144 return CommandLine.Parsed()
1147 // CommandLine is the default set of command-line flags, parsed from os.Args.
1148 // The top-level functions such as BoolVar, Arg, and so on are wrappers for the
1149 // methods of CommandLine.
1150 var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
1153 // Override generic FlagSet default Usage with call to global Usage.
1154 // Note: This is not CommandLine.Usage = Usage,
1155 // because we want any eventual call to use any updated value of Usage,
1156 // not the value it has when this line is run.
1157 CommandLine.Usage = commandLineUsage
1160 func commandLineUsage() {
1164 // NewFlagSet returns a new, empty flag set with the specified name and
1165 // error handling property. If the name is not empty, it will be printed
1166 // in the default usage message and in error messages.
1167 func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
1170 errorHandling: errorHandling,
1172 f.Usage = f.defaultUsage
1176 // Init sets the name and error handling property for a flag set.
1177 // By default, the zero FlagSet uses an empty name and the
1178 // ContinueOnError error handling policy.
1179 func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
1181 f.errorHandling = errorHandling