Documentation
¶
Index ¶
- Constants
- Variables
- type Command
- type CompCmd
- type CompList
- type CompOpt
- type Group
- type Option
- type Options
- func (opt *Options) AddGroup(group string) *Group
- func (opt *Options) Completions() (string, error)
- func (opt *Options) GetBool(name string) bool
- func (opt *Options) GetFloat(name string) float64
- func (opt *Options) GetGroup(name string) *Group
- func (opt *Options) GetGroups() []*Group
- func (opt *Options) GetInt(name string) int
- func (opt *Options) GetOption(name string) *Option
- func (opt *Options) GetPosBool(placeholder string) bool
- func (opt *Options) GetPosFloat(placeholder string) float64
- func (opt *Options) GetPosFloatSlice(placeholder string) []float64
- func (opt *Options) GetPosInt(placeholder string) int
- func (opt *Options) GetPosIntSlice(placeholder string) []int
- func (opt *Options) GetPosString(placeholder string) string
- func (opt *Options) GetPosStringSlice(placeholder string) []string
- func (opt *Options) GetString(name string) string
- func (opt *Options) GetStringSlice(name string) []string
- func (opt *Options) GroupCount() int
- func (opt *Options) HelpOrFail()
- func (opt *Options) HelpOrFailArgs(args []string)
- func (opt *Options) Parse(args []string) error
- func (opt *Options) ParseEnvironment(prefix, delimiter string) error
- func (opt *Options) PrintHelp()
- func (opt *Options) RemoveGroup(name string)
- func (opt *Options) SetCommand(name, help, group string, fn ToolCommand, aliases []string) *Command
- func (opt *Options) SetDefaultHelp(full bool)
- func (opt *Options) SetFlag(group, short, long, help string) error
- func (opt *Options) SetOption(group, short, long, help string, defaultvalue any, required bool, t uint8, ...) error
- func (opt *Options) SetPositional(placeholder, help string, defaultvalue any, required bool, t uint8) error
- func (opt *Options) ShowOptions()
- type ToolCommand
Constants ¶
const ( // VarBool option. VarBool uint8 = iota // VarInt option. VarInt // VarIntSlice option. VarIntSlice // VarFloat option. VarFloat // VarFloatSlice option. VarFloatSlice // VarString option. VarString // VarStringSlice option. VarStringSlice )
Variable types
const (
// GroupDefault is the default group name.
GroupDefault = "default"
)
Variables ¶
var ( // ErrRunCommand is not really an error, but indicates a command was run. ErrRunCommand = errors.New("command run") // ErrNoArgs is returned when no arguments are provided. ErrNoArgs = errors.New("no arguments provided") // ErrIllegalChoice is returned when the argument isn't in the approved list of choices. ErrIllegalChoice = errors.New("illegal choice") // ErrMissingRequired is returned when a required option is missing. ErrMissingRequired = errors.New("missing required option") // ErrMissingParam is returned when an option is missing an argument. ErrMissingParam = errors.New("missing option parameter") // ErrMissingFunc is returned when a command is missing a function. ErrMissingFunc = errors.New("missing function") // ErrLongShort is returned when a short option is longer than one character. ErrLongShort = errors.New("short option must be one character") // ErrUnknownOption is returned when an undefined option is encountered. ErrUnknownOption = errors.New("unknown option") // ErrEmptyLong is returned when a long option is empty. ErrEmptyLong = errors.New("long option without a string") // ErrShortLong is returned when a long option is shorter than two characters. ErrShortLong = errors.New("long option must be at least two characters") // ErrUnknownType is returned when an unknown option variable type is encountered. ErrUnknownType = errors.New("unknown option type") // ErrNoPlaceholder is returned when a positional argument is missing a placeholder. ErrNoPlaceholder = errors.New("no placeholder") )
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { // Name of the command. Name string // Help text of the command. Help string // Func to execute the command. Func ToolCommand // Options for this command. Options *Options // Aliases for this command. Aliases []string }
Command definition.
type CompList ¶ added in v0.3.0
type CompList struct { // AppName fot references. AppName string // Options for the main program. Options []CompOpt // Commands and their options. Commands []CompCmd }
CompList is a list of commands with their options.
type CompOpt ¶ added in v0.3.0
type CompOpt struct { // Name of the option. Name string }
CompOpt is a completion option.
type Group ¶
type Group struct { // Name of the group. Name string // contains filtered or unexported fields }
Group definition.
func (*Group) GetOptions ¶
GetOptions returns a slice of options.
type Option ¶
type Option struct { // Placeholder is the placeholder variable shown in help text. Placeholder string // ShortName of the option. ShortName string // LongName of the option. LongName string // Help text of the option. Help string // Value of the option. Value any // Default value if unspecified. Default any // Choices allowed for the option. Choices []any // Type of value. Type uint8 // Required is true if this must be defined. A default would satisfy this. Required bool }
Option definition.
func (*Option) ValidDefault ¶ added in v0.9.0
ValidDefault returns true if the default value is usable (not the zero-value).
type Options ¶
type Options struct { // Args contains args not parsed as options, commands or positional args. Args []string // contains filtered or unexported fields }
Options base definition.
func (*Options) Completions ¶ added in v0.3.0
Completions returns a string containing the completion script.
func (*Options) GetPosBool ¶
GetPosBool returns a positional boolean's value.
func (*Options) GetPosFloat ¶
GetPosFloat returns a positional float's value.
func (*Options) GetPosFloatSlice ¶
GetPosFloatSlice returns a positional float slice's values.
func (*Options) GetPosIntSlice ¶
GetPosIntSlice returns a positional int slice's values.
func (*Options) GetPosString ¶
GetPosString returns a positional string's value.
func (*Options) GetPosStringSlice ¶
GetPosStringSlice returns a positional string slice's values.
func (*Options) GetStringSlice ¶
GetStringSlice returns a string slice option's value.
func (*Options) GroupCount ¶
GroupCount returns the number of groups.
func (*Options) HelpOrFail ¶ added in v0.11.0
func (opt *Options) HelpOrFail()
HelpOrFail parses the CLI arguments, then prints the help text and exits if the -h flag is set, or prints an error and exits with exit code 2 if something went wrong.
func (*Options) HelpOrFailArgs ¶ added in v0.12.0
HelpOrFailArgs works like HelpOrFail(), but takes a string slice to parse. Use os.Args[1:] to mimic HelpOrFail(), and increase the number for sub-commands.
func (*Options) Parse ¶
Parse command line arguments from a string slice. - If default help is defined, it will print the help message after parsing when "-h" or "--help" is supplied, then os.Exit(0). Returns ErrNoArgs if no arguments are supplied.
Tool commands, short options (single dash and one letter), long options (double dash and one or more letters), and positional arguments are each paarsed in the order they are supplied. If a positional argument is of a slice type, it will swallow all remaining arguments, including long and short options.
Single- and double-dash options found before any tool commands are parsed for the Options structure.
Tool commands break the parsing off, and calls the command with the remaining arguments after running
any handlers for the pre-command options.
Options criteria: - Short options start with a single dash ("-"). - Short boolean options don't need to take a value. - Short boolean options require an equal sign ("=") after the option with a truthy or falsy value. - Truthy values are "true", "yes", "on", "1", and "t". - Falsy values are everything else. - Short options can be combined ("-a -b" can be written as "-ab"). - Combined short options allow only the last one to take a value. The ones before must be booleans.
- Long options start with a double dash ("--"). - Long options are followed by either whitespace or an equal sign ("--foo bar" or "--foo=bar").
func (*Options) ParseEnvironment ¶ added in v0.6.0
ParseEnvironment parses environment variables into options. By default, this parses uppercase versions of long options, but an optional prefix can be provided. So if you provide the prefix "moo" (case doesn't matter) and have a long option "host", this will be set if the envvar "MOO_HOST" is set. Delimiter is used when parsing slices. If unset, it defaults to a comma.
func (*Options) PrintHelp ¶
func (opt *Options) PrintHelp()
PrintHelp builds and prints the help text based on available options.
func (*Options) RemoveGroup ¶
RemoveGroup from map and order.
func (*Options) SetCommand ¶
func (opt *Options) SetCommand(name, help, group string, fn ToolCommand, aliases []string) *Command
SetCommand to a group.
func (*Options) SetDefaultHelp ¶
SetDefaultHelp sets the default help option, optionally with a short "-h" flag.
func (*Options) SetFlag ¶ added in v0.14.0
SetFlag is a shortcut to create a bool option defaulting to false.
func (*Options) SetOption ¶
func (opt *Options) SetOption(group, short, long, help string, defaultvalue any, required bool, t uint8, choices []any) error
SetOption sets an option with parameters, or a flag.
func (*Options) SetPositional ¶
func (opt *Options) SetPositional(placeholder, help string, defaultvalue any, required bool, t uint8) error
SetPositional sets a positional argument. Arguments which aren't long or short options or tool commands are considered positional.
func (*Options) ShowOptions ¶
func (opt *Options) ShowOptions()
ShowOptions shows the values of all options. Used for debugging.