Documentation
¶
Overview ¶
Package cli creates simple CLI options with ENV overrides using viper.
This is a small simplification over viper to move most of the boilerplate into one place.
In this example the flags can be set with MYPROGRAM_MONITOR_HOST and MYPROGRAM_NUMBER or with the flags --monitor-host and --number
var flags struct {
monitorHost string
number int
}
func main() {
cmd := cli.NewCommand(&cli.Program{
Run: run,
Name: "myprogram",
Opts: []cli.Opt{
{
DestP: &flags.monitorHost,
Flag: "monitor-host",
Default: "http://localhost:8086",
Desc: "host to send influxdb metrics",
},
{
DestP: &flags.number,
Flag: "number",
Default: 2,
Desc: "number of times to loop",
},
},
})
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
for i := 0; i < number; i++ {
fmt.Printf("%d\n", i)
feturn nil
}
}
Index ¶
- func BindOptions(cmd *cobra.Command, opts []Opt)
- func IDVar(fs *pflag.FlagSet, p *influxdb.ID, name string, value influxdb.ID, ...)
- func IDVarP(fs *pflag.FlagSet, p *influxdb.ID, name, shorthand string, value influxdb.ID, ...)
- func NewCommand(p *Program) *cobra.Command
- type Opt
- type OrgBucket
- type Program
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindOptions ¶
BindOptions adds opts to the specified command and automatically registers those options with viper.
func IDVar ¶
IDVar defines an influxdb.ID flag with specified name, default value, and usage string. The argument p points to an influxdb.ID variable in which to store the value of the flag.
func IDVarP ¶
func IDVarP(fs *pflag.FlagSet, p *influxdb.ID, name, shorthand string, value influxdb.ID, usage string)
IDVarP is like IDVar, but accepts a shorthand letter that can be used after a single dash.
func NewCommand ¶
NewCommand creates a new cobra command to be executed that respects env vars.
Uses the upper-case version of the program's name as a prefix to all environment variables.
This is to simplify the viper/cobra boilerplate.
Example ¶
var monitorHost string
var number int
var sleep bool
var duration time.Duration
var stringSlice []string
var fancyBool customFlag
cmd := NewCommand(&Program{
Run: func() error {
fmt.Println(monitorHost)
for i := 0; i < number; i++ {
fmt.Printf("%d\n", i)
}
fmt.Println(sleep)
fmt.Println(duration)
fmt.Println(stringSlice)
fmt.Println(fancyBool)
return nil
},
Name: "myprogram",
Opts: []Opt{
{
DestP: &monitorHost,
Flag: "monitor-host",
Default: "http://localhost:8086",
Desc: "host to send influxdb metrics",
},
{
DestP: &number,
Flag: "number",
Default: 2,
Desc: "number of times to loop",
},
{
DestP: &sleep,
Flag: "sleep",
Default: true,
Desc: "whether to sleep",
},
{
DestP: &duration,
Flag: "duration",
Default: time.Minute,
Desc: "how long to sleep",
},
{
DestP: &stringSlice,
Flag: "string-slice",
Default: []string{"foo", "bar"},
Desc: "things come in lists",
},
{
DestP: &fancyBool,
Flag: "fancy-bool",
Default: "on",
Desc: "things that implement pflag.Value",
},
},
})
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
Output: http://localhost:8086 0 1 true 1m0s [foo bar] on
Types ¶
type Opt ¶
type Opt struct {
DestP interface{} // pointer to the destination
EnvVar string
Flag string
Hidden bool
Persistent bool
Required bool
Short rune // using rune b/c it guarantees correctness. a short must always be a string of length 1
Default interface{}
Desc string
}
Opt is a single command-line option