Documentation ¶
Overview ¶
Package flag implements command-line flag parsing.
Usage:
Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -f or --flagname, stored in the pointer ip, with type *int.
import "flag /github.com/docker/docker/pkg/mflag" var ip = flag.Int([]string{"f", "-flagname"}, 1234, "help message for flagname")
If you like, you can bind the flag to a variable using the Var() functions.
var flagvar int func init() { // -flaghidden will work, but will be hidden from the usage flag.IntVar(&flagvar, []string{"f", "#flaghidden", "-flagname"}, 1234, "help message for flagname") }
Or you can create custom flags that satisfy the Value interface (with pointer receivers) and couple them to flag parsing by
flag.Var(&flagVal, []string{"name"}, "help message for flagname")
For such flags, the default value is just the initial value of the variable.
You can also add "deprecated" flags, they are still usable, but are not shown in the usage and will display a warning when you try to use them. `#` before an option means this option is deprecated, if there is an following option without `#` ahead, then that's the replacement, if not, it will just be removed:
var ip = flag.Int([]string{"#f", "#flagname", "-flagname"}, 1234, "help message for flagname")
this will display: `Warning: '-f' is deprecated, it will be replaced by '--flagname' soon. See usage.` or this will display: `Warning: '-flagname' is deprecated, it will be replaced by '--flagname' soon. See usage.`
var ip = flag.Int([]string{"f", "#flagname"}, 1234, "help message for flagname")
will display: `Warning: '-flagname' is deprecated, it will be removed soon. See usage.` so you can only use `-f`.
You can also group one letter flags, bif you declare
var v = flag.Bool([]string{"v", "-verbose"}, false, "help message for verbose") var s = flag.Bool([]string{"s", "-slow"}, false, "help message for slow")
you will be able to use the -vs or -sv
After all flags are defined, call
flag.Parse()
to parse the command line into the defined flags.
Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip) fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments after the flag are available as the slice flag.Args() or individually as flag.Arg(i). The arguments are indexed from 0 through flag.NArg()-1.
Command line flag syntax:
-flag -flag=x -flag="x" -flag='x' -flag x // non-boolean flags only
One or two minus signs may be used; they are equivalent. The last form is not permitted for boolean flags because the meaning of the command
cmd -x *
will change if there is a file called 0, false, etc. You must use the -flag=false form to turn off a boolean flag.
Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator "--".
Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False. Duration flags accept any input valid for time.ParseDuration.
The default set of command-line flags is controlled by top-level functions. The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of FlagSet are analogous to the top-level functions for the command-line flag set.
Index ¶
- Constants
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(names []string, value bool, usage string) *bool
- func BoolVar(p *bool, names []string, value bool, usage string)
- func Duration(names []string, value time.Duration, usage string) *time.Duration
- func DurationVar(p *time.Duration, names []string, value time.Duration, usage string)
- func Float64(names []string, value float64, usage string) *float64
- func Float64Var(p *float64, names []string, value float64, usage string)
- func Int(names []string, value int, usage string) *int
- func Int64(names []string, value int64, usage string) *int64
- func Int64Var(p *int64, names []string, value int64, usage string)
- func IntVar(p *int, names []string, value int, usage string)
- func IsSet(name string) bool
- func NArg() int
- func NFlag() int
- func Parse()
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func String(names []string, value string, usage string) *string
- func StringVar(p *string, names []string, value string, usage string)
- func Uint(names []string, value uint, usage string) *uint
- func Uint64(names []string, value uint64, usage string) *uint64
- func Uint64Var(p *uint64, names []string, value uint64, usage string)
- func UintVar(p *uint, names []string, value uint, usage string)
- func Var(value Value, names []string, usage string)
- func Visit(fn func(*Flag))
- func VisitAll(fn func(*Flag))
- type ErrorHandling
- type Flag
- type FlagSet
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) Bool(names []string, value bool, usage string) *bool
- func (f *FlagSet) BoolVar(p *bool, names []string, value bool, usage string)
- func (f *FlagSet) CheckArgs() (message string)
- func (f *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string)
- func (f *FlagSet) FlagCount() int
- func (f *FlagSet) FlagCountUndeprecated() int
- func (f *FlagSet) Float64(names []string, value float64, usage string) *float64
- func (f *FlagSet) Float64Var(p *float64, names []string, value float64, usage string)
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Int(names []string, value int, usage string) *int
- func (f *FlagSet) Int64(names []string, value int64, usage string) *int64
- func (f *FlagSet) Int64Var(p *int64, names []string, value int64, usage string)
- func (f *FlagSet) IntVar(p *int, names []string, value int, usage string)
- func (f *FlagSet) IsSet(name string) bool
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) NArg() int
- func (f *FlagSet) NFlag() int
- func (f *FlagSet) Name() string
- func (f *FlagSet) Out() io.Writer
- func (f *FlagSet) Parse(arguments []string) error
- func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) PrintDefaults()
- func (cmd *FlagSet) ReportError(str string, withHelp bool)
- func (f *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int)
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) SetOutput(output io.Writer)
- func (f *FlagSet) String(names []string, value string, usage string) *string
- func (f *FlagSet) StringVar(p *string, names []string, value string, usage string)
- func (f *FlagSet) Uint(names []string, value uint, usage string) *uint
- func (f *FlagSet) Uint64(names []string, value uint64, usage string) *uint64
- func (f *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string)
- func (f *FlagSet) UintVar(p *uint, names []string, value uint, usage string)
- func (f *FlagSet) Var(value Value, names []string, usage string)
- func (f *FlagSet) Visit(fn func(*Flag))
- func (f *FlagSet) VisitAll(fn func(*Flag))
- type Getter
- type Value
Constants ¶
const ( Exact nArgRequirementType = iota Max Min )
Indicator used to pass to BadArgs function
Variables ¶
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
CommandLine is the default set of command-line flags, parsed from os.Args. The top-level functions such as BoolVar, Arg, and on are wrappers for the methods of CommandLine.
var ErrHelp = errors.New("flag: help requested")
ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
var ErrRetry = errors.New("flag: retry")
ErrRetry is the error returned if you need to try letter by letter
var ShortUsage = func() { fmt.Fprintf(CommandLine.output, "Usage of %s:\n", os.Args[0]) }
Usage prints to standard error a usage message documenting the standard command layout The function is a variable that may be changed to point to a custom function.
var Usage = func() { fmt.Fprintf(CommandLine.Out(), "Usage of %s:\n", os.Args[0]) PrintDefaults() }
Usage prints to standard error a usage message documenting all defined command-line flags. The function is a variable that may be changed to point to a custom function.
Functions ¶
func Arg ¶
Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed.
func Bool ¶
Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.
func BoolVar ¶
BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.
func Duration ¶
Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag.
func DurationVar ¶
DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag.
func Float64 ¶
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
func Float64Var ¶
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
func Int ¶
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
func Int64 ¶
Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.
func Int64Var ¶
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
func IntVar ¶
IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.
func IsSet ¶ added in v1.6.0
Indicates whether the specified flag was specified at all on the cmd line
func NArg ¶
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func Parse ¶
func Parse()
Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints to standard error the default values of all defined command-line flags.
func String ¶
String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.
func StringVar ¶
StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.
func Uint ¶
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
func Uint64 ¶
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64Var ¶
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
func UintVar ¶
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
func Var ¶
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Types ¶
type ErrorHandling ¶
type ErrorHandling int
ErrorHandling defines how to handle flag parsing errors.
const ( ContinueOnError ErrorHandling = iota ExitOnError PanicOnError )
type Flag ¶
type Flag struct { Names []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 Flag represents the state of a flag.
type FlagSet ¶
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. Usage func() ShortUsage func() // contains filtered or unexported fields }
A FlagSet represents a set of defined flags. The zero value of a FlagSet has no name and has ContinueOnError error handling.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
NewFlagSet returns a new, empty flag set with the specified name and error handling property.
func (*FlagSet) Arg ¶
Arg returns the i'th argument. Arg(0) is the first remaining argument after flags have been processed.
func (*FlagSet) Bool ¶
Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.
func (*FlagSet) BoolVar ¶
BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.
func (*FlagSet) CheckArgs ¶ added in v1.6.0
CheckArgs uses the requirements set by FlagSet.Require() to validate the number of arguments. If the requirements are not met, an error message string is returned.
func (*FlagSet) Duration ¶
Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag.
func (*FlagSet) DurationVar ¶
DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag.
func (*FlagSet) FlagCount ¶ added in v1.3.0
FlagCount returns the number of flags that have been defined.
func (*FlagSet) FlagCountUndeprecated ¶ added in v1.3.0
FlagCountUndeprecated returns the number of undeprecated flags that have been defined.
func (*FlagSet) Float64 ¶
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
func (*FlagSet) Float64Var ¶
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
func (*FlagSet) Init ¶
func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
Init sets the name and error handling property for a flag set. By default, the zero FlagSet uses an empty name and the ContinueOnError error handling policy.
func (*FlagSet) Int ¶
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
func (*FlagSet) Int64 ¶
Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.
func (*FlagSet) Int64Var ¶
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
func (*FlagSet) IntVar ¶
IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.
func (*FlagSet) IsSet ¶ added in v1.6.0
Indicates whether the specified flag was specified at all on the cmd line
func (*FlagSet) Lookup ¶
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) Parse ¶
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help was set but not defined.
func (*FlagSet) ParseFlags ¶ added in v1.7.0
ParseFlags is a utility function that adds a help flag if withHelp is true, calls cmd.Parse(args) and prints a relevant error message if there are incorrect number of arguments. It returns error only if error handling is set to ContinueOnError and parsing fails. If error handling is set to ExitOnError, it's safe to ignore the return value.
func (*FlagSet) PrintDefaults ¶
func (f *FlagSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined flags in the set.
func (*FlagSet) ReportError ¶ added in v1.7.0
func (*FlagSet) Require ¶ added in v1.6.0
Require adds a requirement about the number of arguments for the FlagSet. The first parameter can be Exact, Max, or Min to respectively specify the exact, the maximum, or the minimal number of arguments required. The actual check is done in FlagSet.CheckArgs().
func (*FlagSet) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*FlagSet) String ¶
String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.
func (*FlagSet) StringVar ¶
StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.
func (*FlagSet) Uint ¶
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
func (*FlagSet) Uint64 ¶
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
func (*FlagSet) Uint64Var ¶
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
func (*FlagSet) UintVar ¶
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
func (*FlagSet) Var ¶
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
type Getter ¶
type Getter interface { Value Get() interface{} }
Getter is an interface that allows the contents of a Value to be retrieved. It wraps the Value interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All Value types provided by this package satisfy the Getter interface.