cli

package
v2.0.0-beta.11 Latest Latest
Warning

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

Go to latest
Published: May 26, 2020 License: MIT Imports: 8 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(cmd *cobra.Command, opts []Opt)

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

func IDVar

func IDVar(fs *pflag.FlagSet, p *influxdb.ID, name string, value influxdb.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 *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

func NewCommand(p *Program) *cobra.Command

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
package main

import (
	"fmt"
	"os"
	"time"
)

type customFlag bool

func (c customFlag) String() string {
	if c == true {
		return "on"
	}
	return "off"
}

func (c *customFlag) Set(s string) error {
	if s == "on" {
		*c = true
	} else {
		*c = false
	}

	return nil
}

func (c *customFlag) Type() string {
	return "fancy-bool"
}

func main() {
	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
	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

func NewOpt

func NewOpt(destP interface{}, flag string, dflt interface{}, desc string) Opt

NewOpt creates a new command line option.

type OrgBucket

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

func (*OrgBucket) AddFlags

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

func (*OrgBucket) Name

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

func (*OrgBucket) OrgBucketID

func (o *OrgBucket) OrgBucketID() (orgID, bucketID influxdb.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