flagtag: github.com/cobratbq/flagtag Index | Examples | Files

package flagtag

import "github.com/cobratbq/flagtag"

Package flagtag provides support for creating command line flags by tagging appropriate struct fields with the 'flag' tag.

Index

Examples

Package Files

config.go

func Configure Uses

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 Uses

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 Uses

func ConfigureAndParseArgs(config interface{}, args []string) error

ConfigureAndParseArgs is like ConfigureAndParse with the addition that it is possible to provide the arguments slice that should be parsed.

func ConfigureFlagset Uses

func ConfigureFlagset(config interface{}, flagset *flag.FlagSet) error

ConfigureFlagset is like Configure but with the added ability to provide a flag set.

func ConfigureFlagsetAndParse Uses

func ConfigureFlagsetAndParse(config interface{}, flagset *flag.FlagSet) error

ConfigureFlagsetAndParse is like ConfigureAndParse with the addition that it is possible to provide the flagset for the configuration.

func ConfigureFlagsetAndParseArgs Uses

func ConfigureFlagsetAndParseArgs(config interface{}, flagset *flag.FlagSet, args []string) error

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 Uses

func MustConfigure(config interface{})

MustConfigure is like Configure, the only difference is that it will panic in case of an error.

func MustConfigureAndParse Uses

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.")
}

func MustConfigureAndParseArgs Uses

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 Uses

func MustConfigureFlagset(config interface{}, flagset *flag.FlagSet)

MustConfigureFlagset is like Configure, the only difference being that it is possible to provide a custom flagset.

func MustConfigureFlagsetAndParse Uses

func MustConfigureFlagsetAndParse(config interface{}, flagset *flag.FlagSet)

MustConfigureFlagsetAndParse is like MustConfigureAndParse with the addition that it is possible to provide a custom flagset.

func MustConfigureFlagsetAndParseArgs Uses

func MustConfigureFlagsetAndParseArgs(config interface{}, flagset *flag.FlagSet, args []string)

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 Uses

type ErrInvalidDefault struct {
    // contains filtered or unexported fields
}

ErrInvalidDefault is an error type for the case of invalid defaults.

func (*ErrInvalidDefault) Error Uses

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.