Package flag
import "flag"
The flag package implements command-line flag parsing.
Usage:
1) Define flags using flag.String(), Bool(), Int(), etc. Example:
import "flag"
var ip *int = flag.Int("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() {
flag.IntVar(&flagvar, "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, "name", "help message for flagname")
For such flags, the default value is just the initial value of the variable.
2) After all flags are defined, call
flag.Parse()
to parse the command line into the defined flags.
3) 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);
4) After parsing, flag.Arg(i) is the i'th argument after the flags. Args are indexed from 0 up to flag.NArg().
Command line flag syntax:
-flag -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.
Package files
flag.goVariables
Usage prints to standard error a default usage message documenting all defined flags. The function is a variable that may be changed to point to a custom function.
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
func Arg
func Arg(i int) string
Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed.
func Args
func Args() []string
Args returns the non-flag command-line arguments.
func Bool
func Bool(name string, value bool, usage string) *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
func BoolVar(p *bool, name string, value bool, usage string)
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 Float
func Float(name string, value float, usage string) *float
Float defines a float flag with specified name, default value, and usage string. The return value is the address of a float variable that stores the value of the flag.
func Float64
func Float64(name string, value float64, usage string) *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
func Float64Var(p *float64, name string, value float64, usage string)
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 FloatVar
func FloatVar(p *float, name string, value float, usage string)
FloatVar defines a float flag with specified name, default value, and usage string. The argument p points to a float variable in which to store the value of the flag.
func Int
func Int(name string, value int, usage string) *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
func Int64(name string, value int64, usage string) *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
func Int64Var(p *int64, name string, value int64, usage string)
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
func IntVar(p *int, name string, value int, usage string)
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 NArg
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func NFlag
func NFlag() int
func Parse
func Parse()
Parse parses the command-line flags. Must be called after all flags are defined and before any are accessed by the program.
func ParseForTesting
func ParseForTesting(args []string) (result bool)
ParseForTesting parses the flag state using the provided arguments. It should be called after 1) ResetForTesting and 2) setting up the new flags. The return value reports whether the parse was error-free. For testing only!
func PrintDefaults
func PrintDefaults()
PrintDefaults prints to standard error the default values of all defined flags.
func ResetForTesting
func ResetForTesting(usage func())
ResetForTesting clears all flag state and sets the usage function as directed. After calling ResetForTesting, parse errors in flag handling will panic rather than exit the program. For testing only!
func Set
func Set(name, value string) bool
Set sets the value of the named flag. It returns true if the set succeeded; false if there is no such flag defined.
func String
func String(name, value string, usage string) *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
func StringVar(p *string, name, value string, usage string)
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
func Uint(name string, value uint, usage string) *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
func Uint64(name string, value uint64, usage string) *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
func Uint64Var(p *uint64, name string, value uint64, usage string)
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
func UintVar(p *uint, name string, value uint, usage string)
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
func Var(value Value, name string, usage string)
Var defines a user-typed flag with specified name, default value, and usage string. The argument p points to a Value variable in which to store the value of the flag.
func Visit
func Visit(fn func(*Flag))
Visit visits the flags, calling fn for each. It visits only those flags that have been set.
func VisitAll
func VisitAll(fn func(*Flag))
VisitAll visits the flags, calling fn for each. It visits all flags, even those not set.
type Flag
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
}
func Lookup
func Lookup(name string) *Flag
Lookup returns the Flag structure of the named flag, returning nil if none exists.
type Value
Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)
type Value interface {
String() string
Set(string) bool
}
