README ¶
Description
pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
pflag is compatible with the GNU extensions to the POSIX recommendations for command-line options. For a more precise description, see the "Command-line flag syntax" section below.
pflag is available under the same style of BSD license as the Go language, which can be found in the LICENSE file.
Installation
pflag is available using the standard go get
command.
Install by running:
go get github.com/ogier/pflag
Run tests by running:
go test github.com/ogier/pflag
Usage
pflag is a drop-in replacement of Go's native flag package. If you import pflag under the name "flag" then all code should continue to function with no changes.
import flag "github.com/ogier/pflag"
There is one exception to this: if you directly instantiate the Flag struct there is one more field "Shorthand" that you will need to set. Most code never instantiates this struct directly, and instead uses functions such as String(), BoolVar(), and Var(), and is therefore unaffected.
Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
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.
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.
The pflag package also defines some new functions that are not in flag, that give one-letter shorthands for flags. You can use these by appending 'P' to the name of any function that defines a flag.
var ip = flag.IntP("flagname", "f", 1234, "help message")
var flagvar bool
func init() {
flag.BoolVarP("boolname", "b", true, "help message")
}
flag.VarP(&flagVar, "varname", "v", 1234, "help message")
Shorthand letters can be used with single dashes on the command line. Boolean shorthand flags can be combined with other shorthand flags.
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.
Command line flag syntax
--flag // boolean flags only
--flag=x
Unlike the flag package, a single dash before an option means something different than a double dash. Single dashes signify a series of shorthand letters for flags. All but the last shorthand letter must be boolean flags.
// boolean flags
-f
-abc
// non-boolean flags
-n 1234
-Ifile
// mixed
-abcs "hello"
-abcn1234
Flag parsing stops after the terminator "--". Unlike the flag package, flags can be interspersed with arguments anywhere on the command line before this terminator.
Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags (in their long form) accept 1, 0, t, f, true, false, TRUE, FALSE, True, False. Duration flags accept any input valid for time.ParseDuration.
Mutating or "Normalizing" Flag names
It is possible to set a custom flag name 'normalization function.' It allows flag names to be mutated both when created in the code and when used on the command line to some 'normalized' form. The 'normalized' form is used for comparison. Two examples of using the custom normalization func follow.
Example #1: You want -, _, and . in flags to compare the same. aka --my-flag == --my_flag == --my.flag
func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
from := []string{"-", "_"}
to := "."
for _, sep := range from {
name = strings.Replace(name, sep, to, -1)
}
return pflag.NormalizedName(name)
}
myFlagSet.SetNormalizeFunc(wordSepNormalizeFunc)
Example #2: You want to alias two flags. aka --old-flag-name == --new-flag-name
func aliasNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "old-flag-name":
name = "new-flag-name"
break
}
return pflag.NormalizedName(name)
}
myFlagSet.SetNormalizeFunc(aliasNormalizeFunc)
More info
You can see the full reference documentation of the pflag package
at godoc.org, or through go's standard documentation system by
running godoc -http=:6060
and browsing to
http://localhost:6060/pkg/github.com/ogier/pflag after
installation.
Documentation ¶
Overview ¶
pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
pflag is compatible with the GNU extensions to the POSIX recommendations for command-line options. See http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
Usage:
pflag is a drop-in replacement of Go's native flag package. If you import pflag under the name "flag" then all code should continue to function with no changes.
import flag "github.com/ogier/pflag"
There is one exception to this: if you directly instantiate the Flag struct there is one more field "Shorthand" that you will need to set. Most code never instantiates this struct directly, and instead uses functions such as String(), BoolVar(), and Var(), and is therefore unaffected.
Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
var ip = 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.
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.
The pflag package also defines some new functions that are not in flag, that give one-letter shorthands for flags. You can use these by appending 'P' to the name of any function that defines a flag.
var ip = flag.IntP("flagname", "f", 1234, "help message") var flagvar bool func init() { flag.BoolVarP("boolname", "b", true, "help message") } flag.VarP(&flagVar, "varname", "v", 1234, "help message")
Shorthand letters can be used with single dashes on the command line. Boolean shorthand flags can be combined with other shorthand flags.
Command line flag syntax:
--flag // boolean flags only --flag=x
Unlike the flag package, a single dash before an option means something different than a double dash. Single dashes signify a series of shorthand letters for flags. All but the last shorthand letter must be boolean flags.
// boolean flags -f -abc // non-boolean flags -n 1234 -Ifile // mixed -abcs "hello" -abcn1234
Flag parsing stops after the terminator "--". Unlike the flag package, flags can be interspersed with arguments anywhere on the command line before this terminator.
Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags (in their long form) accept 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 ¶
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- func BoolP(name, shorthand string, value bool, usage string) *bool
- func BoolVar(p *bool, name string, value bool, usage string)
- func BoolVarP(p *bool, name, shorthand string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)
- func Float32(name string, value float32, usage string) *float32
- func Float32P(name, shorthand string, value float32, usage string) *float32
- func Float32Var(p *float32, name string, value float32, usage string)
- func Float32VarP(p *float32, name, shorthand string, value float32, usage string)
- func Float64(name string, value float64, usage string) *float64
- func Float64P(name, shorthand string, value float64, usage string) *float64
- func Float64Var(p *float64, name string, value float64, usage string)
- func Float64VarP(p *float64, name, shorthand string, value float64, usage string)
- func IP(name string, value net.IP, usage string) *net.IP
- func IPMask(name string, value net.IPMask, usage string) *net.IPMask
- func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask
- func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string)
- func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string)
- func IPP(name, shorthand string, value net.IP, usage string) *net.IP
- func IPVar(p *net.IP, name string, value net.IP, usage string)
- func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)
- func Int(name string, value int, usage string) *int
- func Int32(name string, value int32, usage string) *int32
- func Int32P(name, shorthand string, value int32, usage string) *int32
- func Int32Var(p *int32, name string, value int32, usage string)
- func Int32VarP(p *int32, name, shorthand string, value int32, usage string)
- func Int64(name string, value int64, usage string) *int64
- func Int64P(name, shorthand string, value int64, usage string) *int64
- func Int64Var(p *int64, name string, value int64, usage string)
- func Int64VarP(p *int64, name, shorthand string, value int64, usage string)
- func Int8(name string, value int8, usage string) *int8
- func Int8P(name, shorthand string, value int8, usage string) *int8
- func Int8Var(p *int8, name string, value int8, usage string)
- func Int8VarP(p *int8, name, shorthand string, value int8, usage string)
- func IntP(name, shorthand string, value int, usage string) *int
- func IntVar(p *int, name string, value int, usage string)
- func IntVarP(p *int, name, shorthand string, value int, usage string)
- func NArg() int
- func NFlag() int
- func Parse()
- func ParseIPv4Mask(s string) net.IPMask
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func SetInterspersed(interspersed bool)
- func String(name string, value string, usage string) *string
- func StringP(name, shorthand string, value string, usage string) *string
- func StringVar(p *string, name string, value string, usage string)
- func StringVarP(p *string, name, shorthand string, value string, usage string)
- func Uint(name string, value uint, usage string) *uint
- func Uint16(name string, value uint16, usage string) *uint16
- func Uint16P(name, shorthand string, value uint16, usage string) *uint16
- func Uint16Var(p *uint16, name string, value uint16, usage string)
- func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string)
- func Uint32(name string, value uint32, usage string) *uint32
- func Uint32P(name, shorthand string, value uint32, usage string) *uint32
- func Uint32Var(p *uint32, name string, value uint32, usage string)
- func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string)
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64P(name, shorthand string, value uint64, usage string) *uint64
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)
- func Uint8(name string, value uint8, usage string) *uint8
- func Uint8P(name, shorthand string, value uint8, usage string) *uint8
- func Uint8Var(p *uint8, name string, value uint8, usage string)
- func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string)
- func UintP(name, shorthand string, value uint, usage string) *uint
- func UintVar(p *uint, name string, value uint, usage string)
- func UintVarP(p *uint, name, shorthand string, value uint, usage string)
- func Var(value Value, name string, usage string)
- func VarP(value Value, name, shorthand, usage string)
- func Visit(fn func(*Flag))
- func VisitAll(fn func(*Flag))
- type ErrorHandling
- type Flag
- type FlagSet
- func (f *FlagSet) AddFlag(flag *Flag)
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) Bool(name string, value bool, usage string) *bool
- func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool
- func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)
- func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string)
- func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)
- func (f *FlagSet) FlagUsages() string
- func (f *FlagSet) Float32(name string, value float32, usage string) *float32
- func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32
- func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string)
- func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string)
- func (f *FlagSet) Float64(name string, value float64, usage string) *float64
- func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64
- func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string)
- func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string)
- func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName
- func (f *FlagSet) HasFlags() bool
- func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP
- func (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMask
- func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask
- func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string)
- func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string)
- func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP
- func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string)
- func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Int(name string, value int, usage string) *int
- func (f *FlagSet) Int32(name string, value int32, usage string) *int32
- func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32
- func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string)
- func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string)
- func (f *FlagSet) Int64(name string, value int64, usage string) *int64
- func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64
- func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string)
- func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string)
- func (f *FlagSet) Int8(name string, value int8, usage string) *int8
- func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8
- func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string)
- func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string)
- func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int
- func (f *FlagSet) IntVar(p *int, name string, value int, usage string)
- func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string)
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error
- func (f *FlagSet) NArg() int
- func (f *FlagSet) NFlag() int
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) SetInterspersed(interspersed bool)
- func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName)
- func (f *FlagSet) SetOutput(output io.Writer)
- func (f *FlagSet) String(name string, value string, usage string) *string
- func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string
- func (f *FlagSet) StringVar(p *string, name string, value string, usage string)
- func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string)
- func (f *FlagSet) Uint(name string, value uint, usage string) *uint
- func (f *FlagSet) Uint16(name string, value uint16, usage string) *uint16
- func (f *FlagSet) Uint16P(name, shorthand string, value uint16, usage string) *uint16
- func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string)
- func (f *FlagSet) Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string)
- func (f *FlagSet) Uint32(name string, value uint32, usage string) *uint32
- func (f *FlagSet) Uint32P(name, shorthand string, value uint32, usage string) *uint32
- func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string)
- func (f *FlagSet) Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string)
- func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64
- func (f *FlagSet) Uint64P(name, shorthand string, value uint64, usage string) *uint64
- func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)
- func (f *FlagSet) Uint8(name string, value uint8, usage string) *uint8
- func (f *FlagSet) Uint8P(name, shorthand string, value uint8, usage string) *uint8
- func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string)
- func (f *FlagSet) Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string)
- func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint
- func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string)
- func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string)
- func (f *FlagSet) Var(value Value, name string, usage string)
- func (f *FlagSet) VarP(value Value, name, shorthand, usage string)
- func (f *FlagSet) Visit(fn func(*Flag))
- func (f *FlagSet) VisitAll(fn func(*Flag))
- type NormalizedName
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
The default set of command-line flags, parsed from os.Args.
var ErrHelp = errors.New("pflag: help requested")
ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
var Usage = func() { fmt.Fprintf(os.Stderr, "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 DurationP ¶
Like Duration, but accepts a shorthand letter that can be used after a single dash.
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 DurationVarP ¶
Like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func Float32 ¶
Float32 defines a float32 flag with specified name, default value, and usage string. The return value is the address of a float32 variable that stores the value of the flag.
func Float32Var ¶
Float32Var defines a float32 flag with specified name, default value, and usage string. The argument p points to a float32 variable in which to store the value of the flag.
func Float32VarP ¶
Like Float32Var, but accepts a shorthand letter that can be used after a single dash.
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 Float64VarP ¶
Like Float64Var, but accepts a shorthand letter that can be used after a single dash.
func IP ¶
IP defines an net.IP flag with specified name, default value, and usage string. The return value is the address of an net.IP variable that stores the value of the flag.
func IPMask ¶
IPMask defines an net.IPMask flag with specified name, default value, and usage string. The return value is the address of an net.IPMask variable that stores the value of the flag.
func IPMaskVar ¶
IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string. The argument p points to an net.IPMask variable in which to store the value of the flag.
func IPMaskVarP ¶
Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
func IPVar ¶
IPVar defines an net.IP flag with specified name, default value, and usage string. The argument p points to an net.IP 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 Int32 ¶
Int32 defines an int32 flag with specified name, default value, and usage string. The return value is the address of an int32 variable that stores the value of the flag.
func Int32Var ¶
Int32Var defines an int32 flag with specified name, default value, and usage string. The argument p points to an int32 variable in which to store the value of the flag.
func Int32VarP ¶
Like Int32Var, but accepts a shorthand letter that can be used after a single dash.
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 Int64VarP ¶
Like Int64Var, but accepts a shorthand letter that can be used after a single dash.
func Int8 ¶
Int8 defines an int8 flag with specified name, default value, and usage string. The return value is the address of an int8 variable that stores the value of the flag.
func Int8Var ¶
Int8Var defines an int8 flag with specified name, default value, and usage string. The argument p points to an int8 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 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 ParseIPv4Mask ¶
Parse IPv4 netmask written in IP form (e.g. 255.255.255.0). This function should really belong to the net package.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints to standard error the default values of all defined command-line flags.
func SetInterspersed ¶
func SetInterspersed(interspersed bool)
Whether to support interspersed option/non-option arguments.
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 StringVarP ¶
Like StringVar, but accepts a shorthand letter that can be used after a single dash.
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 Uint16 ¶
Uint16 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 Uint16Var ¶
Uint16Var 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 Uint16VarP ¶
Like Uint16Var, but accepts a shorthand letter that can be used after a single dash.
func Uint32 ¶
Uint32 defines a uint32 flag with specified name, default value, and usage string. The return value is the address of a uint32 variable that stores the value of the flag.
func Uint32Var ¶
Uint32Var defines a uint32 flag with specified name, default value, and usage string. The argument p points to a uint32 variable in which to store the value of the flag.
func Uint32VarP ¶
Like Uint32Var, but accepts a shorthand letter that can be used after a single dash.
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 Uint64VarP ¶
Like Uint64Var, but accepts a shorthand letter that can be used after a single dash.
func Uint8 ¶
Uint8 defines a uint8 flag with specified name, default value, and usage string. The return value is the address of a uint8 variable that stores the value of the flag.
func Uint8Var ¶
Uint8Var defines a uint8 flag with specified name, default value, and usage string. The argument p points to a uint8 variable in which to store the value of the flag.
func Uint8VarP ¶
Like Uint8Var, but accepts a shorthand letter that can be used after a single dash.
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 { Name string // name as it appears on command line Shorthand string // one-letter abbreviated flag Usage string // help message Value Value // value as set DefValue string // default value (as text); for usage message Changed bool // If the user set the value (or if left to default) Deprecated string // If this flag is deprecated, this string is the new or now thing to use Annotations map[string][]string // used by cobra.Command bash autocomple code }
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() // contains filtered or unexported fields }
A FlagSet represents a set of defined flags.
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) BoolP ¶
Like Bool, but accepts a shorthand letter that can be used after a single dash.
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) BoolVarP ¶
Like BoolVar, but accepts a shorthand letter that can be used after a single dash.
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) DurationP ¶
func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration
Like Duration, but accepts a shorthand letter that can be used after a single dash.
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) DurationVarP ¶
func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)
Like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) FlagUsages ¶
func (*FlagSet) Float32 ¶
Float32 defines a float32 flag with specified name, default value, and usage string. The return value is the address of a float32 variable that stores the value of the flag.
func (*FlagSet) Float32P ¶
Like Float32, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Float32Var ¶
Float32Var defines a float32 flag with specified name, default value, and usage string. The argument p points to a float32 variable in which to store the value of the flag.
func (*FlagSet) Float32VarP ¶
Like Float32Var, but accepts a shorthand letter that can be used after a single dash.
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) Float64P ¶
Like Float64, but accepts a shorthand letter that can be used after a single dash.
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) Float64VarP ¶
Like Float64Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) GetNormalizeFunc ¶ added in v0.17.0
func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName
func (*FlagSet) IP ¶
IP defines an net.IP flag with specified name, default value, and usage string. The return value is the address of an net.IP variable that stores the value of the flag.
func (*FlagSet) IPMask ¶
IPMask defines an net.IPMask flag with specified name, default value, and usage string. The return value is the address of an net.IPMask variable that stores the value of the flag.
func (*FlagSet) IPMaskP ¶
Like IPMask, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) IPMaskVar ¶
IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string. The argument p points to an net.IPMask variable in which to store the value of the flag.
func (*FlagSet) IPMaskVarP ¶
Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) IPVar ¶
IPVar defines an net.IP flag with specified name, default value, and usage string. The argument p points to an net.IP variable in which to store the value of the flag.
func (*FlagSet) IPVarP ¶
Like IPVar, but accepts a shorthand letter that can be used after a single dash.
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) Int32 ¶
Int32 defines an int32 flag with specified name, default value, and usage string. The return value is the address of an int32 variable that stores the value of the flag.
func (*FlagSet) Int32P ¶
Like Int32, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Int32Var ¶
Int32Var defines an int32 flag with specified name, default value, and usage string. The argument p points to an int32 variable in which to store the value of the flag.
func (*FlagSet) Int32VarP ¶
Like Int32Var, but accepts a shorthand letter that can be used after a single dash.
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) Int64P ¶
Like Int64, but accepts a shorthand letter that can be used after a single dash.
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) Int64VarP ¶
Like Int64Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Int8 ¶
Int8 defines an int8 flag with specified name, default value, and usage string. The return value is the address of an int8 variable that stores the value of the flag.
func (*FlagSet) Int8P ¶
Like Int8, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Int8Var ¶
Int8Var defines an int8 flag with specified name, default value, and usage string. The argument p points to an int8 variable in which to store the value of the flag.
func (*FlagSet) Int8VarP ¶
Like Int8Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) IntP ¶
Like Int, but accepts a shorthand letter that can be used after a single dash.
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) IntVarP ¶
Like IntVar, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Lookup ¶
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) MarkDeprecated ¶ added in v0.17.0
Mark a flag deprecated in your program
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) 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) SetInterspersed ¶
Whether to support interspersed option/non-option arguments.
func (*FlagSet) SetNormalizeFunc ¶ added in v0.17.0
func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName)
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) StringP ¶
Like String, but accepts a shorthand letter that can be used after a single dash.
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) StringVarP ¶
Like StringVar, but accepts a shorthand letter that can be used after a single dash.
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) Uint16 ¶
Uint16 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) Uint16P ¶
Like Uint16, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Uint16Var ¶
Uint16Var 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) Uint16VarP ¶
Like Uint16Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Uint32 ¶
Uint32 defines a uint32 flag with specified name, default value, and usage string. The return value is the address of a uint32 variable that stores the value of the flag.
func (*FlagSet) Uint32P ¶
Like Uint32, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Uint32Var ¶
Uint32Var defines a uint32 flag with specified name, default value, and usage string. The argument p points to a uint32 variable in which to store the value of the flag.
func (*FlagSet) Uint32VarP ¶
Like Uint32Var, but accepts a shorthand letter that can be used after a single dash.
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) Uint64P ¶
Like Uint64, but accepts a shorthand letter that can be used after a single dash.
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) Uint64VarP ¶
Like Uint64Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Uint8 ¶
Uint8 defines a uint8 flag with specified name, default value, and usage string. The return value is the address of a uint8 variable that stores the value of the flag.
func (*FlagSet) Uint8P ¶
Like Uint8, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Uint8Var ¶
Uint8Var defines a uint8 flag with specified name, default value, and usage string. The argument p points to a uint8 variable in which to store the value of the flag.
func (*FlagSet) Uint8VarP ¶
Like Uint8Var, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) UintP ¶
Like Uint, but accepts a shorthand letter that can be used after a single dash.
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) UintVarP ¶
Like UintVar, but accepts a shorthand letter that can be used after a single dash.
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.
func (*FlagSet) VarP ¶
Like Var, but accepts a shorthand letter that can be used after a single dash.
type NormalizedName ¶ added in v0.17.0
type NormalizedName string
NormalizedName is a flag name that has been normalized according to rules for the FlagSet (e.g. making '-' and '_' equivalent).