clix

package module
v0.0.0-...-f43cf22 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 5 Imported by: 16

README

Package clix implements companion functions for github.com/urfave/cli/v2

GoDoc

Documentation

Overview

Package clix implements companion functions for github.com/urfave/cli.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrExclusiveFlagsInArgs represents an error
	// where more than one exclusive flags are set
	// at the same time.
	ErrExclusiveFlags = errors.New("more than one flags are set")

	// ErrExclusiveFlagsInArgs represents an error
	// where more than one exclusive flags are set in arguments
	// at the same time.
	ErrExclusiveFlagsInArgs = fmt.Errorf("%w in args", ErrExclusiveFlags)

	// ErrExclusiveFlagsInEnvs represents an error
	// where more than one exclusive flags are set in environment variables
	// at the same time.
	ErrExclusiveFlagsInEnvs = fmt.Errorf("%w in envs", ErrExclusiveFlags)
)

Functions

func Chain

func Chain(fn ...func(*cli.Context) error) func(*cli.Context) error

Chain returns a function of type `func(*cli.Context) error` in which each fn is called in order.

Example
flagSet := clix.NewFlagSet()
app := cli.NewApp()
app.Before = clix.Chain(flagSet.Init, init1)
Output:

func FilePath

func FilePath(file ...string) string

FilePath returns the first file that exists or an empty string if no file exists.

Example
// This flag can be set in 3 ways.
//
//   1. a command line argument `--password`
//   2. an environment variable `EXAMPLE_PASSWORD`
//   3. one of the first file that exists
//     1. pathname by the environment variable `EXAMPLE_PASSWORD_FILE`
//     2. or pathname of `$HOME/.config/example/defaults/password`
//     3. or pathname of `/etc/example/defaults/password`
flag := &cli.StringFlag{
	Name:    "password",
	EnvVars: []string{"EXAMPLE_PASSWORD"},
	FilePath: clix.FilePath(
		os.Getenv("EXAMPLE_PASSWORD_FILE"),
		os.ExpandEnv("$HOME/.config/example/defaults/password"),
		"/etc/example/defaults/password",
	),
}
_ = flag
Output:

func FlagIf

func FlagIf(cond bool, flag ...cli.Flag) []cli.Flag

FlagIf returns flag if cond is true, otherwise returns nil.

func Flags

func Flags(flags ...interface{}) []cli.Flag

Flags returns []cli.Flag in which flags are flatten, each element of flags must be nil or a cli.Flag or []cli.Flag, panic if none of them. A slice []cli.Flag can contain nil.

func ShortFlagName

func ShortFlagName(name string) string

ShortFlagName makes a short flag name.

e.g.
"target-id" => "ti
"LONG_NAME" => "LN"

Types

type ExclusiveFlags

type ExclusiveFlags struct {
	FlagSet FlagSet
	Flags   []cli.Flag
}

ExclusiveFlags is pair of FlagSet and Flags.

func (ExclusiveFlags) Select

func (ef ExclusiveFlags) Select(cb map[cli.Flag]func() error) error

Select calls the callback function of the exclusive flag that is set, returns the result of the callback.

type FlagName

type FlagName struct {
	Name     string
	Aliases  []string
	EnvVars  []string
	FilePath string
}

func NewFlagName

func NewFlagName(prefix FlagPrefix, group, name string) *FlagName

NewFlagName returns *FlagName.

func NewFlagNameAlias

func NewFlagNameAlias(prefix FlagPrefix, group, name, alias string) *FlagName

NewFlagNameAlias returns *FlagName.

type FlagPrefix

type FlagPrefix string

FlagPrefix represents the prefix of flags.

func (FlagPrefix) EnvVars

func (fp FlagPrefix) EnvVars(s ...string) []string

EnvVars returns []string{FlagPrefix + s, ...}.

func (FlagPrefix) FilePath

func (fp FlagPrefix) FilePath(s ...string) string

FilePath returns clix.FilePath(os.Getenv(FlagPrefix + s + "_FILE"), ...).

func (FlagPrefix) String

func (fp FlagPrefix) String() string

String returns string(fp).

type FlagSet

type FlagSet map[string]struct{}

FlagSet represents ...

func NewFlagSet

func NewFlagSet() FlagSet

NewFlagSet returns a FlagSet.

func (FlagSet) Exclusive

func (fs FlagSet) Exclusive(flag ...cli.Flag) (cli.Flag, error)

Exclusive returns the flag only if it is set exclusively or nil if none is set, returns err!=nil if and only if multiple flags are set at the same time.

At first command line arguments are searched. Next the environment variables (include FilePath) are searched. At most one flag can be specified at the same time, otherwise err!=nil is returned.

func (FlagSet) Init

func (fs FlagSet) Init(c *cli.Context) error

Init initialize fs using c.LocalFlagNames().

func (FlagSet) IsSet

func (fs FlagSet) IsSet(flag cli.Flag) bool

IsSet returns true if flag is specified or false otherwise. `specified` includes following casese.

  • specified by arguments
  • specified by EnvVars
  • specified by FilePath

func (FlagSet) IsSetArgs

func (fs FlagSet) IsSetArgs(flag cli.Flag) bool

IsSetArgs returns true if flag is specified in arguments or false otherwise.

func (FlagSet) NewExclusiveFlags

func (fs FlagSet) NewExclusiveFlags(flag ...cli.Flag) *ExclusiveFlags

NewExclusiveFlags returns ExclusiveFlags.

func (FlagSet) Select

func (fs FlagSet) Select(flags []cli.Flag, cb map[cli.Flag]func() error) error

Select calls the callback function for exclusive flag that is set, returns the result of the callback.

Jump to

Keyboard shortcuts

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