Documentation
¶
Overview ¶
Package flagtag provides support for creating command line flags by tagging appropriate struct fields with the 'flag' tag.
Index ¶
- func Configure(config interface{}) error
- func ConfigureAndParse(config interface{}) error
- func ConfigureAndParseArgs(config interface{}, args []string) error
- func ConfigureFlagset(config interface{}, flagset *flag.FlagSet) error
- func ConfigureFlagsetAndParse(config interface{}, flagset *flag.FlagSet) error
- func ConfigureFlagsetAndParseArgs(config interface{}, flagset *flag.FlagSet, args []string) error
- func MustConfigure(config interface{})
- func MustConfigureAndParse(config interface{})
- func MustConfigureAndParseArgs(config interface{}, args []string)
- func MustConfigureFlagset(config interface{}, flagset *flag.FlagSet)
- func MustConfigureFlagsetAndParse(config interface{}, flagset *flag.FlagSet)
- func MustConfigureFlagsetAndParseArgs(config interface{}, flagset *flag.FlagSet, args []string)
- type ErrInvalidDefault
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Configure ¶
func Configure(config interface{}) error
Configure will configure the flag parameters according to the tags of the provided data type. It is allowed to call this method multiple times with different data types. (As long as flag's Parse() method has not been called yet.) Fields without a 'flag' tag or with an empty 'flag' tag will be ignored.
The 'flag' tag consists of 3 parts, similar to the *Var-functions of the flag package. Parts are separated by a comma. The parts are:
- 1st: flag name
- 2nd: default value
- 3rd: usage description
Example:
`flag:"verbose,false,Enable verbose output."`.
This will create a flag 'verbose', which defaults to 'false' and shows usage information "Enable verbose output.".
If an error occurs, this error will be returned and the configuration of other struct fields will be aborted.
func ConfigureAndParse ¶
func ConfigureAndParse(config interface{}) error
ConfigureAndParse will first attempt to configure the flags according to the provided config type. If any error occurs, this error will be returned and the command line arguments will not be parsed. If no error occurs, the command line arguments will be parsed and the config type will contain the result. Using this function may remove the need to even import the flag package at all.
func ConfigureAndParseArgs ¶
ConfigureAndParseArgs is like ConfigureAndParse with the addition that it is possible to provide the arguments slice that should be parsed.
func ConfigureFlagset ¶
ConfigureFlagset is like Configure but with the added ability to provide a flag set.
func ConfigureFlagsetAndParse ¶
ConfigureFlagsetAndParse is like ConfigureAndParse with the addition that it is possible to provide the flagset for the configuration.
func ConfigureFlagsetAndParseArgs ¶
ConfigureFlagsetAndParseArgs is like ConfigureAndParse with the addition that it is possible to provide both the flagset for configuration and the arguments slice that should be parsed.
func MustConfigure ¶
func MustConfigure(config interface{})
MustConfigure is like Configure, the only difference is that it will panic in case of an error.
func MustConfigureAndParse ¶
func MustConfigureAndParse(config interface{})
MustConfigureAndParse is like ConfigureAndParse, the only difference is that it will panic in case of an error.
Example (Greeter) ¶
// Prepare configuration var config struct { Greeting string `flag:"greet,Hello,The greeting."` Name string `flag:"name,User,The user's name."` Times int `flag:"times,1,Number of repeats."` } MustConfigureAndParse(&config) // Start greeting for i := 0; i < config.Times; i++ { fmt.Printf("%s %s!\n", config.Greeting, config.Name) }
Output:
Example (Sleep) ¶
// Prepare configuration var config struct { Sleep time.Duration `flag:"s,1s,The amount of time to sleep."` Verbose bool `flag:"v,false,Verbose output."` } MustConfigureAndParse(&config) // Start sleeping. if config.Verbose { log.Println("Sleeping for " + config.Sleep.String()) } time.Sleep(config.Sleep) if config.Verbose { log.Println("Done.") }
Output:
func MustConfigureAndParseArgs ¶
func MustConfigureAndParseArgs(config interface{}, args []string)
MustConfigureAndParseArgs is like MustConfigureAndParse with the addition that it is possible to provide an arguments slice to be parsed, instead of the default command line arguments slice.
func MustConfigureFlagset ¶
MustConfigureFlagset is like Configure, the only difference being that it is possible to provide a custom flagset.
func MustConfigureFlagsetAndParse ¶
MustConfigureFlagsetAndParse is like MustConfigureAndParse with the addition that it is possible to provide a custom flagset.
func MustConfigureFlagsetAndParseArgs ¶
MustConfigureFlagsetAndParseArgs is like MustConfigureAndParse with the addition that it is possible to provide both a custom flagset and the argument slice to be parsed.
Types ¶
type ErrInvalidDefault ¶
type ErrInvalidDefault struct {
// contains filtered or unexported fields
}
ErrInvalidDefault is an error type for the case of invalid defaults.
func (*ErrInvalidDefault) Error ¶
func (e *ErrInvalidDefault) Error() string
Error returns the error explaining the bad default value.