pflag
v1.1.0
Documentation
pkg.go.dev ↗Package 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/spf13/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(&flagvar, "boolname", "b", true, "help message")
}
flag.VarP(&flagval, "varname", "v", "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.
Variables
CommandLine
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
CommandLine is the default set of command-line flags, parsed from os.Args.
ErrHelp
var ErrHelp = errors.New("pflag: help requested")
ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
Usage
var Usage = func() {
_, _ = fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
Usage prints, to the set destination, a usage message documenting all defined command-line flags. The function is a variable that may be changed to point to a custom function. By default it prints a simple header and calls PrintDefaults; for details about the format of the output and how to control it, see the documentation for PrintDefaults.
Functions
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.
Args
func Args() []string
Args returns the non-flag command-line arguments.
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.
BoolFunc
func BoolFunc(name string, usage string, fn func(string) error)
BoolFunc defines a func flag with specified name, callback function and usage string.
The callback function will be called every time "--{name}" (or any form that matches the flag) is parsed on the command line.
BoolFuncP
func BoolFuncP(name, shorthand string, usage string, fn func(string) error)
BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash.
BoolP
func BoolP(name, shorthand string, value bool, usage string) *bool
BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
BoolSlice
func BoolSlice(name string, value []bool, usage string) *[]bool
BoolSlice 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.
BoolSliceP
func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool
BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash.
BoolSliceVar
func BoolSliceVar(p *[]bool, name string, value []bool, usage string)
BoolSliceVar 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.
BoolSliceVarP
func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string)
BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash.
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.
BoolVarP
func BoolVarP(p *bool, name, shorthand string, value bool, usage string)
BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
BytesBase64
func BytesBase64(name string, value []byte, usage string) *[]byte
BytesBase64 defines an []byte flag with specified name, default value, and usage string. The return value is the address of an []byte variable that stores the value of the flag.
BytesBase64P
func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte
BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
BytesBase64Var
func BytesBase64Var(p *[]byte, name string, value []byte, usage string)
BytesBase64Var defines an []byte flag with specified name, default value, and usage string. The argument p points to an []byte variable in which to store the value of the flag.
BytesBase64VarP
func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string)
BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
BytesHex
func BytesHex(name string, value []byte, usage string) *[]byte
BytesHex defines an []byte flag with specified name, default value, and usage string. The return value is the address of an []byte variable that stores the value of the flag.
BytesHexP
func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte
BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
BytesHexVar
func BytesHexVar(p *[]byte, name string, value []byte, usage string)
BytesHexVar defines an []byte flag with specified name, default value, and usage string. The argument p points to an []byte variable in which to store the value of the flag.
BytesHexVarP
func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string)
BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
Count
func Count(name string, usage string) *int
Count defines a count 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. A count flag will add 1 to its value every time it is found on the command line
CountP
func CountP(name, shorthand string, usage string) *int
CountP is like Count only takes a shorthand for the flag name.
CountVar
func CountVar(p *int, name string, usage string)
CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
CountVarP
func CountVarP(p *int, name, shorthand string, usage string)
CountVarP is like CountVar only take a shorthand for the flag name.
Duration
func Duration(name string, value time.Duration, usage string) *time.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.
DurationP
func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration
DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
DurationSlice
func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
DurationSlice 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.
DurationSliceP
func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration
DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash.
DurationSliceVar
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
DurationSliceVar defines a duration[] flag with specified name, default value, and usage string. The argument p points to a duration[] variable in which to store the value of the flag.
DurationSliceVarP
func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string)
DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash.
DurationVar
func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
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.
DurationVarP
func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)
DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
Float32
func Float32(name string, value float32, usage string) *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.
Float32P
func Float32P(name, shorthand string, value float32, usage string) *float32
Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
Float32Slice
func Float32Slice(name string, value []float32, usage string) *[]float32
Float32Slice 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.
Float32SliceP
func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32
Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash.
Float32SliceVar
func Float32SliceVar(p *[]float32, name string, value []float32, usage string)
Float32SliceVar 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.