Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadFlagFromArgs ¶
LoadFlagFromArgs will load a flag from cli args. Returns true if the flag was found, false otherwise. If the flag was found, value will contain the value provided for the flag, if any. If the flag was found with a '=' and no value after the '=' an error will be returned.
Types ¶
type App ¶
type App struct { Name string // for documentation only (should match the name of the executable) Description string // used for documentation SubCmds []Cmd // a list of available sub commands ReqFlags []Flag // required flags: if not provided, the cli will print an error, the help doc, and exit OptFlags []Flag // optional flags: if not provided, the default value will be used. Note that the help flag is automatically added to this list. Args []string // positional args: used to populate the args map passed to the action function. Action func(args map[string]string) }
type BoolFlag ¶ added in v1.1.0
BoolFlag is a flag that if provided, the value will be true. If not provided, the value will be false.
func (*BoolFlag) GetDescription ¶ added in v1.1.0
type Cmd ¶
type Cmd struct { Name string Alias string // can be used instead of Name (ideally 1 or 2 characters) Description string // used for documentation SubCmds []Cmd // sub commands list ReqFlags []Flag // required flags: if not provided, the cli will print an error, the help doc, and exit OptFlags []Flag // optional flags: if not provided, the default value will be used. Note that the help flag is automatically added to this list. Args []string // positional args: used to populate the args map passed to the action function. Action func(args map[string]string) // contains filtered or unexported fields }
Cmd represents a command in the cli. A command can have sub commands, flags, and positional arguments.
type Flag ¶
type Flag interface { GetName() string GetAlias() string GetDescription() string // Load a flag. The argFound bool indicates if the flag was found in the cli args // and argVal contains the value provided for the flag, if any. // Should return true if the flag was loaded (used for required/optional validation) // Returns an error if the flag was found but the value was invalid etc. // This method can be used to load the flag from any source, not just cli args. Eg: env vars, files etc. Load(argFound bool, argVal *string) (loaded bool, err error) }
Flag allows for custom flag types to be created. (Although the included flag types should be sufficient for most cases.) Command line flags are expected in one of the following formats:
--flag=value --flag='value' --flag="value" --flag
Or using an alias:
-f=value -f='value' -f="value" -f
type IntFlag ¶
type IntFlag struct { Name string Alias string Description string Value int // can provide a default value here }
IntFlag can be provided by cli args or env var. Values are parsed as int. CLI args take precedence.
func (*IntFlag) GetDescription ¶
type JSONFlag ¶
type JSONFlag[T any] struct { Name string Alias string EnvVar string Description string Value T // can provide a default value here }
JSONFlag will parse a json string into Value, of type T. The value can be provided by cli args or env var. CLI args take precedence.
func (*JSONFlag[T]) GetDescription ¶
type StringFlag ¶
type StringFlag struct { Name string Alias string EnvVar string Description string AcceptedValues []string // if specified, only these values are accepted Value string // can provide a default value here }
StringFlag is a flag that which can be provided by cli args or env var. CLI args take precedence. If AcceptedValues are specified, the value is validated against them.
func (*StringFlag) GetAlias ¶
func (flag *StringFlag) GetAlias() string
func (*StringFlag) GetDescription ¶
func (flag *StringFlag) GetDescription() string
func (*StringFlag) GetName ¶
func (flag *StringFlag) GetName() string