Documentation ¶
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 NArg() int
- func NFlag() int
- func Parse()
- func ParseAll(fn func(flag *Flag, value string) error)
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func SetInterspersed(interspersed bool)
- func UnquoteUsage(flag *Flag) (name string, 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) AddFlagSet(newSet *FlagSet)
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) ArgsLenAtDash() int
- 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) Changed(name string) bool
- func (f *FlagSet) FlagUsages() string
- func (f *FlagSet) FlagUsagesWrapped(cols int) string
- func (f *FlagSet) GetBool(name string) (bool, error)
- func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName
- func (f *FlagSet) HasAvailableFlags() bool
- func (f *FlagSet) HasFlags() bool
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error
- func (f *FlagSet) MarkHidden(name string) error
- func (f *FlagSet) MarkShorthandDeprecated(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) ParseAll(arguments []string, fn func(flag *Flag, value string) error) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) SetAnnotation(name, key string, values []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) ShorthandLookup(name string) *Flag
- func (f *FlagSet) Var(value Value, name string, usage string)
- func (f *FlagSet) VarP(value Value, name, shorthand, usage string)
- func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag
- func (f *FlagSet) Visit(fn func(*Flag))
- func (f *FlagSet) VisitAll(fn func(*Flag))
- type NormalizedName
- type ParseErrorsWhitelist
- type SliceValue
- 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.
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. 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 ¶
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 BoolP ¶
BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
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 is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
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 ParseAll ¶
ParseAll parses the command-line flags from os.Args[1:] and called fn for each. The arguments for fn are flag and value. 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 SetInterspersed ¶
func SetInterspersed(interspersed bool)
SetInterspersed sets whether to support interspersed option/non-option arguments.
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for a flag and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the flag's value, or the empty string if the flag is boolean.
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 will return an err from Parse() if an error is found ContinueOnError ErrorHandling = iota // ExitOnError will call os.Exit(2) if an error is found when parsing ExitOnError // PanicOnError will panic() if an error is found when parsing flags 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) NoOptDefVal string // default value (as text); if the flag is on the command line without any options Deprecated string // If this flag is deprecated, this string is the new or now thing to use Hidden bool // used by cobra.Command to allow flags to be hidden from help/usage text ShorthandDeprecated string // If the shorthand of 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.
func Lookup ¶
Lookup returns the Flag structure of the named command-line flag, returning nil if none exists.
func ShorthandLookup ¶
ShorthandLookup returns the Flag structure of the short handed flag, returning nil if none exists.
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() // SortFlags is used to indicate, if user wants to have sorted flags in // help/usage messages. SortFlags bool // ParseErrorsWhitelist is used to configure a whitelist of errors ParseErrorsWhitelist ParseErrorsWhitelist // 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, error handling property and SortFlags set to true.
func (*FlagSet) AddFlagSet ¶
AddFlagSet adds one FlagSet to another. If a flag is already present in f the flag from newSet will be ignored.
func (*FlagSet) Arg ¶
Arg returns the i'th argument. Arg(0) is the first remaining argument after flags have been processed.
func (*FlagSet) ArgsLenAtDash ¶
ArgsLenAtDash will return the length of f.Args at the moment when a -- was found during arg parsing. This allows your program to know which args were before the -- and which came after.
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 is 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 ¶
BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func (*FlagSet) Changed ¶
Changed returns true if the flag was explicitly set during Parse() and false otherwise
func (*FlagSet) FlagUsages ¶
FlagUsages returns a string containing the usage information for all flags in the FlagSet
func (*FlagSet) FlagUsagesWrapped ¶
FlagUsagesWrapped returns a string containing the usage information for all flags in the FlagSet. Wrapped to `cols` columns (0 for no wrapping)
func (*FlagSet) GetNormalizeFunc ¶
func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName
GetNormalizeFunc returns the previously set NormalizeFunc of a function which does no translation, if not set previously.
func (*FlagSet) HasAvailableFlags ¶
HasAvailableFlags returns a bool to indicate if the FlagSet has any flags that are not hidden.
func (*FlagSet) HasFlags ¶
HasFlags returns a bool to indicate if the FlagSet has any flags defined.
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) Lookup ¶
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) MarkDeprecated ¶
MarkDeprecated indicated that a flag is deprecated in your program. It will continue to function but will not show up in help or usage messages. Using this flag will also print the given usageMessage.
func (*FlagSet) MarkHidden ¶
MarkHidden sets a flag to 'hidden' in your program. It will continue to function but will not show up in help or usage messages.
func (*FlagSet) MarkShorthandDeprecated ¶
MarkShorthandDeprecated will mark the shorthand of a flag deprecated in your program. It will continue to function but will not show up in help or usage messages. Using this flag will also print the given usageMessage.
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) ParseAll ¶
ParseAll parses flag definitions from the argument list, which should not include the command name. The arguments for fn are flag and value. 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) SetAnnotation ¶
SetAnnotation allows one to set arbitrary annotations on a flag in the FlagSet. This is sometimes used by spf13/cobra programs which want to generate additional bash completion information.
func (*FlagSet) SetInterspersed ¶
SetInterspersed sets whether to support interspersed option/non-option arguments.
func (*FlagSet) SetNormalizeFunc ¶
func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName)
SetNormalizeFunc allows you to add a function which can translate flag names. Flags added to the FlagSet will be translated and then when anything tries to look up the flag that will also be translated. So it would be possible to create a flag named "getURL" and have it translated to "geturl". A user could then pass "--getUrl" which may also be translated to "geturl" and everything will work.
func (*FlagSet) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*FlagSet) ShorthandLookup ¶
ShorthandLookup returns the Flag structure of the short handed flag, returning nil if none exists. It panics, if len(name) > 1.
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 ¶
VarP is like Var, but accepts a shorthand letter that can be used after a single dash.
type NormalizedName ¶
type NormalizedName string
NormalizedName is a flag name that has been normalized according to rules for the FlagSet (e.g. making '-' and '_' equivalent).
type ParseErrorsWhitelist ¶
type ParseErrorsWhitelist struct { // UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags UnknownFlags bool }
ParseErrorsWhitelist defines the parsing errors that can be ignored
type SliceValue ¶
type SliceValue interface { // Append adds the specified value to the end of the flag value list. Append(string) error // Replace will fully overwrite any data currently in the flag value list. Replace([]string) error // GetSlice returns the flag value list as an array of strings. GetSlice() []string }
SliceValue is a secondary interface to all flags which hold a list of values. This allows full control over the value of list flags, and avoids complicated marshalling and unmarshalling to csv.