Documentation ¶
Overview ¶
Package multiflag implements the flag.Value interface for handling repeated flag values.
It is useful for boolean flags where repeated use implies greater intensity or values to be collected into an array.
Usage:
Imports, definitions, etc:
import ( "flag" "github.com/gyepisam/multiflag" )
A boolean variable counts flags and does not consume any arguments.
var verbosity = multiflag.Bool("verbose", "false", "Verbosity. Repeat as necessary", "v")
String variables consume and collect their arguments into a string array.
var trace = multiflag.String("trace", "none", "Trace program sections", "t")
After calling
flag.Parse()
You can get the count
fmt.Println("Verbosity:", verbosity.NArg())
which, given the flags:
-v -v -verbose --verbose
produces the output:
Verbosity: 4
or the arguments
for _, item := range trace.Args() { fmt.Println("Tracing:", item) }
which, given the flags:
-t parse -trace compile
produces the output:
Tracing: parse Tracing: compile
The examples above can be found in the file main/main.go, which can also be compiled and run. It has the following usage text:
Usage of main: -t=none: Alias for trace -trace=none: Trace program sections -v=false: Alias for verbose -verbose=false: Verbosity. Repeat as necessary
multiflag also works with *flag.FlagSet instances. The previous example would require the following changes:
fs := flag.NewFlagSet("subcommand", flag.ContinueOnError) var verbosity = multiflag.BoolSet(fs, "verbose", "false", "Verbosity. Repeat as necessary", "v") var trace = multiflag.StringSet(fs, "trace", "none", "Trace program sections", "t")
Index ¶
- type AliasUsageFunc
- type Flagger
- type Value
- func Bool(name string, value string, usage string, aliases ...string) *Value
- func BoolSet(flg *flag.FlagSet, name string, value string, usage string, aliases ...string) *Value
- func String(name string, value string, usage string, aliases ...string) *Value
- func StringSet(flg *flag.FlagSet, name string, value string, usage string, aliases ...string) *Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AliasUsageFunc ¶
AliasUsageFunc specifies the signature for an alias usage function.
var AliasUsage AliasUsageFunc = func(orig, alias string) string {
return "Alias for " + orig
}
AliasUsage returns the usage text for an alias. The function is a variable that may be changed to point to a custom function of type AliasUsageFunc.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value counts and collects repeated uses of a flag.
func Bool ¶
Bool returns a boolean multiflag instance associated with flag.. name, value, and usage are used to initial a flag.Value. aliases, if any, initialize aliases for name. See AliasUsage.
func BoolSet ¶
BoolSet creates a boolean multiflag instance, associates it with the provided FlagSet and returns it.
func String ¶
String returns a string multiflag instance associated with flag. name, value, and usage are used to initial a flag.Value. aliases, if any, initialize aliases for name. See AliasUsage.
func StringSet ¶
func StringSet(flg *flag.FlagSet, name string, value string, usage string, aliases ...string) *Value
StringSet creates a string multiflag instance, associates it with the provided FlagSet and returns it.
func (*Value) Args ¶
Args returns an array of collected arguments. A Bool always returns an empty array.
func (*Value) IsBoolFlag ¶
IsBoolFlag returns a value denoting whether the variable represents a boolean value. Provided for flag package.