Documentation ¶
Index ¶
- Variables
- func Add(flags ...Flag)
- func AddSet(s Set)
- func AddSets(S ...Set)
- func Arg(i int) string
- func Args() []string
- func NArg() int
- func Parse()
- func ParseWithErr() error
- func SetGlobalFooter(s string)
- func SetGlobalHeader(s string)
- func SetName(name string)
- func SetUsageFooter(s string)
- func SetUsageHeader(s string)
- func SetValidate(f func() error)
- func Usage()
- type BasicSet
- func (S *BasicSet) Add(f ...Flag)
- func (S *BasicSet) Flags() []Flag
- func (S *BasicSet) Name() string
- func (S *BasicSet) SetUsageFooter(s string)
- func (S *BasicSet) SetUsageHeader(s string)
- func (S *BasicSet) SetValidate(f func() error)
- func (S *BasicSet) UsageFooter() string
- func (S *BasicSet) UsageHeader() string
- func (S *BasicSet) Validate() error
- type Flag
- func Bool(name string, b *bool, def bool, description string, usage string) Flag
- func Duration(name string, d *time.Duration, def time.Duration, description string, ...) Flag
- func Func(name string, description string, usage string, fn func(string) error) Flag
- func Int(name string, n *int, def int, description string, usage string) Flag
- func Path(name string, s *string, def string, description string, usage string) Flag
- func String(name string, s *string, def string, description string, usage string) Flag
- func StringNoDefault(name string, s *string, description string, usage string) Flag
- func TextVar(name string, p encoding.TextUnmarshaler, value encoding.TextMarshaler, ...) Flag
- type Set
Constants ¶
This section is empty.
Variables ¶
var ErrHelp = errors.New("flag: help requested")
ErrHelp is returned if the help flag is specified.
Functions ¶
func Arg ¶
Arg returns the i-th command-line argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist.
func NArg ¶
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func Parse ¶
func Parse()
Parse parses flags and command-line arguments. It then calls Validate for each registered flag set in turn. On parse or validation error, Parse prints the error and a usage message to os.Stderr and exits (via os.Exit) with a non-zero error code. If the help flag (-h or -help) is provided then Parse prints a usage message to os.Stderr and exits (via os.Exit) with error code zero. For more control of behaviour on error, see ParseWithErr. Exactly one of Parse and ParseWithErr should be called, after all flags and sets have been registered.
func ParseWithErr ¶
func ParseWithErr() error
ParseWithErr parses flags and command-line arguments. It then calls Validate for each registered flag set in turn. It returns ErrHelp if the help flag (-h or -help) was present. Exactly one of Parse and ParseWithErr should be called, after all flags and sets have been registered.
func SetGlobalFooter ¶
func SetGlobalFooter(s string)
SetGlobalFooter sets the global footer for the usage message.
func SetGlobalHeader ¶
func SetGlobalHeader(s string)
SetGlobalHeader sets the global header for the usage message.
func SetUsageFooter ¶
func SetUsageFooter(s string)
SetUsageFooter sets the footer in the usage message for the standard flag set.
func SetUsageHeader ¶
func SetUsageHeader(s string)
SetUsageHeader sets the header in the usage message for the standard flag set.
func SetValidate ¶
func SetValidate(f func() error)
SetValidate sets the validation function for the standard flag set
Types ¶
type BasicSet ¶
type BasicSet struct {
// contains filtered or unexported fields
}
BasicSet is a basic flag set.
func NewBasicSet ¶
NewBasicSet defines a FlagSet with the given name and flags, and trivial Validate method.
func (*BasicSet) Flags ¶
Flags returns the flags in the set, in lexicographic order. If there are no flags in the set, it returns a non-nil empty slice.
func (*BasicSet) SetUsageFooter ¶
SetUsageFooter sets the footer text for the usage message for this flag set.
func (*BasicSet) SetUsageHeader ¶
SetUsageHeader sets the header text for the usage message for this flag set.
func (*BasicSet) SetValidate ¶
SetValidate sets the validation function.
func (*BasicSet) UsageFooter ¶
UsageFooter returns the footer text in the usage message (if any).
func (*BasicSet) UsageHeader ¶
UsageHeader returns the header text in the usage message (if any).
type Flag ¶
type Flag interface { Name() string // Name returns the flag name (without leading hyphen). Description() string // Description returns a one-line description of this variable. Usage() string // Usage returns long-form usage text (or the empty string if not required). Parse(string) error // Parse parses the string. }
Flag is the interface a flag satisfies.
A Flag can optionally satisfy the interface
type isBooleaner struct { // IsBoolean returns true if and only if this flag is a boolean flag in the sense of the flag package. IsBoolean() bool }
to indicate whether or not it is a boolean flag in the sense of the flag package.
A Flag can optionally satisfy the interface
type alternativeNamer interface { // AlternativeName returns an alternative flag name (without leading // hyphen). AlternativeName() string }
to provide an additional flag name (without leading hyphen) to Name.
func Bool ¶
Bool returns a Flag that represents a boolean flag with the given name, description, usage string, backing variable, and default value.
func Duration ¶
func Duration(name string, d *time.Duration, def time.Duration, description string, usage string) Flag
Duration returns a Flag that represents a time.Duration-valued flag with the given name, description, and usage string. It has backing variable d, and default value def.
func Func ¶ added in v0.0.19
Func returns a Flag with the given name, description, and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.
func Int ¶
Int returns a Flag that represents an integer flag with the given name, description, usage string, backing variable, and default value.
func Path ¶
Path returns a Flag that represents a file path flag with the given name, description, and usage string. It has backing variable s, and default value def.
func String ¶
String returns a Flag that represents a string-valued flag with the given name, description, and usage string. It has backing variable s, and default value def.
func StringNoDefault ¶ added in v0.0.16
StringNoDefault returns a Flag that represents a string-valued flag with the given name, description, and usage string. It has backing variable s.
func TextVar ¶ added in v0.0.19
func TextVar(name string, p encoding.TextUnmarshaler, value encoding.TextMarshaler, description string, usage string) Flag
TextVar returns a Flag with the given name, description, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.
type Set ¶
type Set interface { Flags() []Flag // Flags returns the members of the set, in lexicographic order. Name() string // Name returns the name of this collection of flags (or the empty string if not required). UsageHeader() string // UsageHeader returns the header for the usage message for this flag set. Validate() error // Validate validates this flag set. }
Set is the interface describing a set of flags
func Merge ¶
Merge amalgamates the flag sets in S, returning them as a single flag set with the given name. The usage header and footer messages are concatenated. The Validate method of the returned flag set is obtained by calling s.Validate() for each member s of S in turn and returning the first non-nil result (if any).