Documentation ¶
Overview ¶
Package flag provides a command-line flag parser. Unlike the Go standard library flag package, it permits flags to be interspersed with arguments.
Example ¶
package main import ( "fmt" "gg-scm.io/tool/internal/flag" ) func main() { fset := new(flag.FlagSet) x := fset.Bool("x", false, "boolean flag") output := fset.String("o", "", "output path (string flag)") if err := fset.Parse([]string{"-o", "/path/to/foo", "input", "-x"}); err != nil { panic(err) } fmt.Println("x =", *x) fmt.Println("output =", *output) fmt.Println("args =", fset.Args()) }
Output: x = true output = /path/to/foo args = [input]
Index ¶
- func IsHelp(e error) bool
- type FlagSet
- func (f *FlagSet) Alias(name string, aliases ...string)
- 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) BoolVar(p *bool, name string, value bool, usage string)
- func (f *FlagSet) Help(w io.Writer)
- func (f *FlagSet) Init(intersperse bool, usage, description string)
- func (f *FlagSet) Int(name string, value int, usage string) *int
- func (f *FlagSet) IntVar(p *int, name string, value int, usage string)
- func (f *FlagSet) MultiString(name string, usage string) *[]string
- func (f *FlagSet) MultiStringVar(p *[]string, name string, usage string)
- func (f *FlagSet) NArg() int
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) Regexp(name string, usage string) **regexp.Regexp
- func (f *FlagSet) RegexpVar(p **regexp.Regexp, name string, usage string)
- func (f *FlagSet) String(name string, value string, usage string) *string
- func (f *FlagSet) StringVar(p *string, name string, value string, usage string)
- func (f *FlagSet) Var(value Value, name string, usage string)
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
A FlagSet represents a set of defined flags. The zero value of a FlagSet is an empty set of flags, allows non-flag arguments to be interspersed with flags, and has no usage information.
func NewFlagSet ¶
NewFlagSet returns a new, empty flag set with the specified intersperse behavior and usage information.
func (*FlagSet) Arg ¶
Arg returns the i'th argument. Arg(0) is the first non-flag argument. Arg returns an empty string if the requested element does not exist.
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) 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) Init ¶
Init sets the argument intersperse behavior and usage information for a flag set. By default, the zero FlagSet allows non-flag arguments to be interspersed with flags.
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) 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) MultiString ¶
MultiString defines a string flag with specified name and usage string. The return value is the address of a string slice variable that stores the value of each passed flag.
func (*FlagSet) MultiStringVar ¶
MultiStringVar defines a string flag with specified name and usage string. The argument p points to a []string variable in which to store the value of each passed flag.
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 returned error can be tested with IsHelpUndefined if -help or -h were set but not defined.
func (*FlagSet) Regexp ¶ added in v1.3.0
Regexp defines a regular expression flag with specified name and usage string. If the flag is given multiple times, then the resulting regular expression is equivalent to joining all the arguments with "|". The return value is the address of a *regexp.Regexp variable that will be nil if no arguments are given.
func (*FlagSet) RegexpVar ¶ added in v1.3.0
RegexpVar defines a regular expression flag with specified name and usage string. If the flag is given multiple times, then the resulting regular expression is equivalent to joining all the arguments with "|". The argument p points to a *regexp.Regexp variable in which to store the resulting regular expression.
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.
type Value ¶
type Value interface { // String presents the current value as a string. String() string // Set is called once, in command line order, for each flag present. Set(string) error // Get returns the contents of the Value. Get() interface{} // If IsBoolFlag returns true, then the command-line parser makes // -name equivalent to -name=true rather than using the next // command-line argument. IsBoolFlag() bool }
Value is the interface to the dynamic value stored in a flag.