Documentation ¶
Overview ¶
Package gflag implements GNU-style command line flag parsing. It supports the transformation of programs using the Go standard library flag package. However it doesn't target full compatibility with the Go standard library flag package. The Flag structure doesn't support all fields of the flag package and the Var method and function does have a different signature.
The typical use case looks like this:
b := Bool("flag-b", "b", false, "boolean flag") h := Bool("help", "h", false, "prints this message") Parse() if *h { gflag.Usage() }
Index ¶
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- func BoolP(name, shorthands string, value bool, usage string) *bool
- func BoolVar(p *bool, name string, value bool, usage string)
- func BoolVarP(p *bool, name, shorthands string, value bool, usage string)
- func Counter(name string, value int, usage string) *int
- func CounterP(name, shorthands string, value int, usage string) *int
- func CounterVar(p *int, name string, value int, usage string)
- func CounterVarP(p *int, name, shorthands string, value int, usage string)
- func Int(name string, value int, usage string) *int
- func IntP(name, shorthands string, value int, usage string) *int
- func IntVar(p *int, name string, value int, usage string)
- func IntVarP(p *int, name, shorthands string, value int, usage string)
- func NArg() int
- func Parse()
- func Parsed() bool
- func Preset(start, end, value int, usage string) *int
- func PresetVar(p *int, start, end, value int, usage string)
- func PrintDefaults()
- func String(name, value, usage string) *string
- func StringP(name, shorthands, value, usage string) *string
- func StringVar(p *string, name, value, usage string)
- func StringVarP(p *string, name, shorthands, value, usage string)
- func Var(value Value, name string, hasArg HasArg)
- func VarP(value Value, name, shorthands string, hasArg HasArg)
- type ErrorHandling
- type Flag
- type FlagSet
- 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, shorthands 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, shorthands string, value bool, usage string)
- func (f *FlagSet) Counter(name string, value int, usage string) *int
- func (f *FlagSet) CounterP(name, shorthands string, value int, usage string) *int
- func (f *FlagSet) CounterVar(p *int, name string, value int, usage string)
- func (f *FlagSet) CounterVarP(p *int, name, shorthands string, value int, usage string)
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Int(name string, value int, usage string) *int
- func (f *FlagSet) IntP(name, shorthands 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, shorthands string, value int, usage string)
- func (f *FlagSet) NArg() int
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) Preset(start, end, value int, usage string) *int
- func (f *FlagSet) PresetVar(p *int, start, end, value int, usage string)
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) SetOutput(w io.Writer)
- func (f *FlagSet) String(name, value, usage string) *string
- func (f *FlagSet) StringP(name, shorthands, value, usage string) *string
- func (f *FlagSet) StringVar(p *string, name, value, usage string)
- func (f *FlagSet) StringVarP(p *string, name, shorthands, value, usage string)
- func (f *FlagSet) Var(value Value, name string, hasArg HasArg)
- func (f *FlagSet) VarP(value Value, name, shorthands string, hasArg HasArg)
- type HasArg
- type Value
Constants ¶
This section is empty.
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, etc. are wrappers for the methods of command line.
var Usage = func() { fmt.Fprintf(CommandLine.out(), "Usage of %s:\n", os.Args[0]) PrintDefaults() }
Usage prints the default usage message.
Functions ¶
func Args ¶
func Args() []string
Args returns all arguments after the command line flags have been parsed.
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 BoolP ¶
BoolP defines a bool flag with specified name, shorthands, 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 BoolVarP ¶
BoolVarP defines a bool flag with specified name, shorthands, default value and usage string. The argument p points to a bool variable in which to store the value of the flag.
func Counter ¶
Counter defines a counter flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func CounterP ¶
CounterP defines a counter flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func CounterVar ¶
CounterVar defines a counter flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func CounterVarP ¶
CounterVarP defines a counter flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func Int ¶
Int defines an integer flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func IntP ¶
IntP defines an integer flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func IntVar ¶
IntVar defines an integer flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func IntVarP ¶
IntVarP defines an integer flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func NArg ¶
func NArg() int
NArg returns the number of remaining arguments after command line parsing.
func Preset ¶
Preset defines a range of preset flags starting at start and ending at end. The return value is the address of a preset variable in which to store the value of the flag.
If start is 1 and end is 9 the flags -1 to -9 will be supported.
func PresetVar ¶
PresetVar defines a range of preset flags starting at start and ending at end. The argument p points to a preset variable in which to store the value of the flag.
If start is 1 and end is 9 the flags -1 to -9 will be supported.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints the information about all 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 StringP ¶
StringP defines a string flag with specified name, shorthands, 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 ¶
StringVarP defines an string flag with specified name, shorthands, default value and usage string. The argument p points to a string variable in which to store the value of the flag.
Types ¶
type ErrorHandling ¶
type ErrorHandling int
ErrorHandling defines how flag parsing errors are handled.
const ( ContinueOnError ErrorHandling = iota ExitOnError PanicOnError )
The constants define how errors should be handled.
type FlagSet ¶
type FlagSet struct { // Provides a custom usage function if set. Usage func() // contains filtered or unexported fields }
FlagSet represents a set of option flags.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
NewFlagSet creates a new flag set.
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 ¶
BoolP defines a bool flag with specified name, shorthands, 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) BoolVarP ¶
BoolVarP defines a bool flag with specified name, shorthands, default value and usage string. The argument p points to a bool variable in which to store the value of the flag.
func (*FlagSet) Counter ¶
Counter defines a counter flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func (*FlagSet) CounterP ¶
CounterP defines a counter flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func (*FlagSet) CounterVar ¶
CounterVar defines a counter flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func (*FlagSet) CounterVarP ¶
CounterVarP defines a counter flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func (*FlagSet) Init ¶
func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
Init initializes a flag set variable.
func (*FlagSet) Int ¶
Int defines an integer flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func (*FlagSet) IntP ¶
IntP defines an integer flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.
func (*FlagSet) IntVar ¶
IntVar defines an integer flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func (*FlagSet) IntVarP ¶
IntVarP defines an integer flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.
func (*FlagSet) Parse ¶
Parse parses the arguments. If an error happens the error is printed as well as the usage information.
func (*FlagSet) Preset ¶
Preset defines a range of preset flags starting at start and ending at end. The return value is the address of a preset variable in which to store the value of the flag.
If start is 1 and end is 9 the flags -1 to -9 will be supported.
func (*FlagSet) PresetVar ¶
PresetVar defines a range of preset flags starting at start and ending at end. The argument p points to a preset variable in which to store the value of the flag.
If start is 1 and end is 9 the flags -1 to -9 will be supported.
func (*FlagSet) PrintDefaults ¶
func (f *FlagSet) PrintDefaults()
PrintDefaults prints information about all flags.
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 ¶
StringP defines a string flag with specified name, shorthands, 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) StringVarP ¶
StringVarP defines an string flag with specified name, shorthands, default value and usage string. The argument p points to a string variable in which to store the value of the flag.