import "github.com/cobratbq/flagtag"
Package flagtag provides support for creating command line flags by tagging appropriate struct fields with the 'flag' tag.
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.
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.
ConfigureAndParseArgs is like ConfigureAndParse with the addition that it is possible to provide the arguments slice that should be parsed.
ConfigureFlagset is like Configure but with the added ability to provide a flag set.
ConfigureFlagsetAndParse is like ConfigureAndParse with the addition that it is possible to provide the flagset for the configuration.
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(config interface{})
MustConfigure is like Configure, the only difference is that it will panic in case of an error.
func MustConfigureAndParse(config interface{})
MustConfigureAndParse is like ConfigureAndParse, the only difference is that it will panic in case of an error.
Code:
// 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) }
Code:
// 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.") }
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.
MustConfigureFlagset is like Configure, the only difference being that it is possible to provide a custom flagset.
MustConfigureFlagsetAndParse is like MustConfigureAndParse with the addition that it is possible to provide a custom flagset.
MustConfigureFlagsetAndParseArgs is like MustConfigureAndParse with the addition that it is possible to provide both a custom flagset and the argument slice to be parsed.
type ErrInvalidDefault struct {
// contains filtered or unexported fields
}
ErrInvalidDefault is an error type for the case of invalid defaults.
func (e *ErrInvalidDefault) Error() string
Error returns the error explaining the bad default value.
Package flagtag imports 8 packages (graph) and is imported by 1 packages. Updated 2016-07-21. Refresh now. Tools for package owners.