Documentation
¶
Overview ¶
Package commandeer sets up command line flags based on the fields and field tags of a struct. It helps ease common pains of CLI app development by allowing you to unobtrusively define flags in a library package while having a tiny main package which calls commandeer.Run* or commandeer.Flags.
Run is the usual interface to commandeer, but it requires you to pass in a struct which has a "Run() error" method. RunArgs works similarly, but allows you to pass in the args to be parsed and the flag set to be used. In cases where your struct doesn't have a Run() method, or you don't want to call it, the Flags() function takes in a FlagSet and sets the flags based on the passed in struct in the same way.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flags ¶
Flags sets up the given Flagger (usually an instance of flag.FlagSet or pflag.FlagSet). The second argument, "main", must be a pointer to a struct. A flag will be created for each exported field of the struct which isn't explicitly ignored.
Struct tags are used to control the behavior of Flags(), though none are necessary.
1. The "help" tag on a field is used to populate the usage string for that field's flag.
2. The "flag" tag on a field will be used as the name of that field's flag. Set it to "-" to ignore this field when creating flags. If it does not exist, the "json" tag will be used, and if it also does not exist, the field name will be downcased and converted from camel case to be dash separated.
3. The "short" tag on a field will be used as the shorthand flag for that field. It should be a single ascii character. This will only be used if the Flagger is also a PFlagger.
func ImplementsRunner ¶
ImplementsRunner returns true if "t" implements the Runner interface and false otherwise.
Types ¶
type Flagger ¶
type Flagger interface { Parse([]string) error StringVar(p *string, name string, value string, usage string) IntVar(p *int, name string, value int, usage string) Int64Var(p *int64, name string, value int64, usage string) BoolVar(p *bool, name string, value bool, usage string) UintVar(p *uint, name string, value uint, usage string) Uint64Var(p *uint64, name string, value uint64, usage string) Float64Var(p *float64, name string, value float64, usage string) DurationVar(p *time.Duration, name string, value time.Duration, usage string) }
Flagger is an interface satisfied by flag.FlagSet and other implementations of flags.
type PFlagger ¶
type PFlagger interface { Parse([]string) error StringSliceVarP(p *[]string, name string, shorthand string, value []string, usage string) BoolSliceVarP(p *[]bool, name string, shorthand string, value []bool, usage string) UintSliceVarP(p *[]uint, name string, shorthand string, value []uint, usage string) IntSliceVarP(p *[]int, name string, shorthand string, value []int, usage string) IPSliceVarP(p *[]net.IP, name string, shorthand string, value []net.IP, usage string) Float32VarP(p *float32, name string, shorthand string, value float32, usage string) IPMaskVarP(p *net.IPMask, name string, shorthand string, value net.IPMask, usage string) IPNetVarP(p *net.IPNet, name string, shorthand string, value net.IPNet, usage string) IPVarP(p *net.IP, name string, shorthand string, value net.IP, usage string) Int32VarP(p *int32, name string, shorthand string, value int32, usage string) Uint16VarP(p *uint16, name string, shorthand string, value uint16, usage string) Uint32VarP(p *uint32, name string, shorthand string, value uint32, usage string) Uint8VarP(p *uint8, name string, shorthand string, value uint8, usage string) Int8VarP(p *int8, name string, shorthand string, value int8, usage string) StringVarP(p *string, name string, shorthand string, value string, usage string) IntVarP(p *int, name string, shorthand string, value int, usage string) Int64VarP(p *int64, name string, shorthand string, value int64, usage string) BoolVarP(p *bool, name string, shorthand string, value bool, usage string) UintVarP(p *uint, name string, shorthand string, value uint, usage string) Uint64VarP(p *uint64, name string, shorthand string, value uint64, usage string) Float64VarP(p *float64, name string, shorthand string, value float64, usage string) DurationVarP(p *time.Duration, name string, shorthand string, value time.Duration, usage string) }
PFlagger is an extension of the Flagger interface which is implemented by the pflag package (github.com/ogier/pflag, or github.com/spf13/pflag)