// A Flag represents the state of a flag. type Flag struct { Name string// name as it appears on command line Usage string// help message Value Value // value as set DefValue string// default value (as text); for usage message }
// A FlagSet represents a set of defined flags. The zero value of a FlagSet // has no name and has ContinueOnError error handling. // // Flag names must be unique within a FlagSet. An attempt to define a flag whose // name is already in use will cause a panic. type FlagSet struct { // Usage is the function called when an error occurs while parsing flags. // The field is a function (not a method) that may be changed to point to // a custom error handler. What happens after Usage is called depends // on the ErrorHandling setting; for the command line, this defaults // to ExitOnError, which exits the program after calling Usage. Usage func()
name string parsed bool actual map[string]*Flag formal map[string]*Flag args []string// arguments after flags errorHandling ErrorHandling output io.Writer // nil means stderr; use out() accessor }
// CommandLine is the default set of command-line flags, parsed from os.Args. // The top-level functions such as BoolVar, Arg, and so on are wrappers for the // methods of CommandLine. var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
1 2 3 4 5 6 7 8 9 10 11
// NewFlagSet returns a new, empty flag set with the specified name and // error handling property. If the name is not empty, it will be printed // in the default usage message and in error messages. funcNewFlagSet(name string, errorHandling ErrorHandling) *FlagSet { f := &FlagSet{ name: name, errorHandling: errorHandling, } f.Usage = f.defaultUsage return f }