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(v *viper.Viper, cmd *cobra.Command, opts []Opt) error
- func IDVar(fs *pflag.FlagSet, p *platform.ID, name string, value platform.ID, ...)
- func IDVarP(fs *pflag.FlagSet, p *platform.ID, name, shorthand string, value platform.ID, ...)
- func LevelVar(fs *pflag.FlagSet, p *zapcore.Level, name string, value zapcore.Level, ...)
- func LevelVarP(fs *pflag.FlagSet, p *zapcore.Level, name, shorthand string, ...)
- func NewCommand(v *viper.Viper, p *Program) (*cobra.Command, error)
- 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 *platform.ID, name, shorthand string, value platform.ID, usage string)
IDVarP is like IDVar, but accepts a shorthand letter that can be used after a single dash.
func LevelVar ¶ added in v2.0.4
LevelVar defines a zapcore.Level flag with specified name, default value, and usage string. The argument p points to a zapcore.Level variable in which to store the value of the flag.
func LevelVarP ¶ added in v2.0.4
func LevelVarP(fs *pflag.FlagSet, p *zapcore.Level, name, shorthand string, value zapcore.Level, usage string)
LevelVarP is like LevelVar, 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 smallerNumber int32 var longerNumber int64 var sleep bool var duration time.Duration var stringSlice []string var fancyBool customFlag var logLevel zapcore.Level cmd, err := NewCommand(viper.New(), &Program{ Run: func() error { fmt.Println(monitorHost) for i := 0; i < number; i++ { fmt.Printf("%d\n", i) } fmt.Println(longerNumber - int64(smallerNumber)) fmt.Println(sleep) fmt.Println(duration) fmt.Println(stringSlice) fmt.Println(fancyBool) fmt.Println(logLevel.String()) 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: &smallerNumber, Flag: "smaller-number", Default: math.MaxInt32, Desc: "limited size number", }, { DestP: &longerNumber, Flag: "longer-number", Default: math.MaxInt64, Desc: "explicitly expanded-size number", }, { 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", }, { DestP: &logLevel, Flag: "log-level", Default: zapcore.WarnLevel, }, }, }) if err != nil { _, _ = fmt.Fprintln(os.Stderr, err) return } cmd.SetArgs([]string{}) if err := cmd.Execute(); err != nil { _, _ = fmt.Fprintln(os.Stderr, err) }
Output: http://localhost:8086 0 1 9223372034707292160 true 1m0s [foo bar] on warn
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