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