cli

package
v2.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2022 License: MIT Imports: 11 Imported by: 1

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindOptions

func BindOptions(v *viper.Viper, cmd *cobra.Command, opts []Opt) error

BindOptions adds opts to the specified command and automatically registers those options with viper.

func IDVar

func IDVar(fs *pflag.FlagSet, p *platform.ID, name string, value platform.ID, usage string)

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

func LevelVar(fs *pflag.FlagSet, p *zapcore.Level, name string, value zapcore.Level, usage string)

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

func NewCommand(v *viper.Viper, p *Program) (*cobra.Command, error)

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

type OrgBucket

type OrgBucket struct {
	Org    platform.ID
	Bucket platform.ID
}

func (*OrgBucket) AddFlags

func (o *OrgBucket) AddFlags(cmd *cobra.Command)

func (*OrgBucket) Name

func (o *OrgBucket) Name() [platform.IDLength]byte

func (*OrgBucket) OrgBucketID

func (o *OrgBucket) OrgBucketID() (orgID, bucketID platform.ID)

type Program

type Program struct {
	// Run is invoked by cobra on execute.
	Run func() error
	// Name is the name of the program in help usage and the env var prefix.
	Name string
	// Opts are the command line/env var options to the program
	Opts []Opt
}

Program parses CLI options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL