Documentation ¶
Overview ¶
Package clix implements companion functions for github.com/urfave/cli.
Index ¶
- Variables
- func Chain(fn ...func(*cli.Context) error) func(*cli.Context) error
- func FilePath(file ...string) string
- func FlagIf(cond bool, flag ...cli.Flag) []cli.Flag
- func Flags(flags ...interface{}) []cli.Flag
- func ShortFlagName(name string) string
- type ExclusiveFlags
- type FlagName
- type FlagPrefix
- type FlagSet
- func (fs FlagSet) Exclusive(flag ...cli.Flag) (cli.Flag, error)
- func (fs FlagSet) Init(c *cli.Context) error
- func (fs FlagSet) IsSet(flag cli.Flag) bool
- func (fs FlagSet) IsSetArgs(flag cli.Flag) bool
- func (fs FlagSet) NewExclusiveFlags(flag ...cli.Flag) *ExclusiveFlags
- func (fs FlagSet) Select(flags []cli.Flag, cb map[cli.Flag]func() error) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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"), ...).
type FlagSet ¶
type FlagSet map[string]struct{}
FlagSet represents ...
func (FlagSet) Exclusive ¶
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) IsSet ¶
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 ¶
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.