Documentation ¶
Index ¶
- Constants
- Variables
- func CheckExistence[S ~[]RM, RM RequirementsMeeter](ctx Context, rms S) (err error)
- func CheckURL(url string) (err error)
- func Close(c io.Closer, f func(err error))
- func CommandString(rootCmd *Command, tokens Tokens) (cs string, cnt int, err error)
- func EmptyStateSatisfied[S ~[]RM, RM RequirementsMeeter](ctx Context, tt TokenType, rms S) (err error)
- func ExecHelp(Context, *CommandInvoker) (err error)
- func ExecutableFilepath(name string) string
- func MeetsRequirements[S ~[]RM, RM RequirementsMeeter](ctx Context, tt TokenType, rms S) (err error)
- func StdErr(msg string, args ...any)
- func StdOut(msg string, args ...any)
- func ValidateByFunc[S ~[]RM, RM RequirementsMeeter](ctx Context, rms S) (err error)
- func WarnOnError(err error)
- type Arg
- func (arg Arg) Check(requires ArgRequires) bool
- func (arg Arg) CheckExistence(ctx Context) (err error)
- func (arg Arg) ClearCheckFunc() Arg
- func (arg Arg) DefaultHelp(opts HelpOpts) (help string)
- func (arg Arg) EmptyOk() Arg
- func (arg Arg) EmptyStateSatisfied(ctx Context, tt TokenType) (err error)
- func (arg Arg) Help(opts HelpOpts) (help string)
- func (arg Arg) IgnoreExists() Arg
- func (arg Arg) IsZero() bool
- func (arg Arg) MustBeEmpty() Arg
- func (arg Arg) MustExist() Arg
- func (arg Arg) MustValidate() Arg
- func (arg Arg) NotEmpty() Arg
- func (arg Arg) NotExist() Arg
- func (arg Arg) SignatureHelp() (s string)
- func (arg Arg) String() string
- func (arg Arg) Unique() string
- func (arg Arg) ValidateByFunc(ctx Context) (err error)
- type ArgName
- type ArgRequires
- type Args
- type ArgsMap
- type Command
- func AddCommand(name Token) (cmd *Command)
- func AddCommandWithFunc(name Token, ef ExecFunc) (cmd *Command)
- func CommandByName(rootCmd *Command, name string) (cmd *Command, depth int)
- func InvokedCommand(rootCmd *Command, tokens Tokens) (_ *Command, _ int, err error)
- func NewCommand(name Token, ef ExecFunc) *Command
- func (c *Command) AddArg(arg Arg) (cmd *Command)
- func (c *Command) AddFlag(flg Flag) (cmd *Command)
- func (c *Command) AddSubCommand(name Token, ef ExecFunc) (cmd *Command)
- func (c *Command) DeclaredArgsCount() (cnt int)
- func (c *Command) FullName() (name string)
- func (c *Command) Help() string
- func (c *Command) InvokedFlags() (flags Flags)
- func (c *Command) IsLeaf() bool
- func (c *Command) MeetsRequirements(ctx Context, tokenCnt int) (err error)
- func (c *Command) OptionalArgsCount() (cnt int)
- func (c *Command) ReceivedArgsCount(tokenCnt int) int
- func (c *Command) RequiredArgsCount() (cnt int)
- func (c *Command) SetFlagDefault(name, value string)
- func (c *Command) SetFlags(flags Flags) Flags
- func (c *Command) SignatureHelp() string
- func (c *Command) String() string
- func (c *Command) Unique() (s string)
- type CommandInvoker
- func (i *CommandInvoker) ArgInt(name ArgName) (n int)
- func (i *CommandInvoker) ArgString(name ArgName) (s string)
- func (i *CommandInvoker) ArgValue(name ArgName) (value *Value)
- func (i *CommandInvoker) Args() Args
- func (i *CommandInvoker) InvokeCommand(ctx Context) (err error)
- func (i *CommandInvoker) MeetsRequirements(ctx Context) (err error)
- func (i *CommandInvoker) SubCommands() CommandMap
- type CommandMap
- type CommandType
- type Context
- type ExecFunc
- type Flag
- type Flags
- func (flags Flags) DisplayWidth(minWidth int) (width int)
- func (flags Flags) Helpers() (helpers []helper)
- func (flags Flags) Index(name ArgName) (n int)
- func (flags Flags) Initialize(ctx Context) Flags
- func (flags Flags) Len() int
- func (flags Flags) Remove(n int) Flags
- func (flags Flags) SignatureHelp() (s string)
- type FlagsMap
- type Help
- type HelpOpts
- type Params
- type RequirementsMeeter
- type RequirementsMeeters
- type StringMap
- type Token
- type TokenType
- type Tokens
- type Value
Constants ¶
const ( CommandArg ArgName = "command" HelpArg ArgName = "help" Indent string = " " )
Variables ¶
var ( ErrOptionAfterArgs = serr.New("option found after args").ValidArgs("options", "args") ErrNoCommandSpecified = serr.New("no command specified") ErrCommandNotValid = serr.New("command is not valid").ValidArgs("command") ErrNoExecFuncFound = serr.New("no exec func found") ErrTooFewArgsPassed = serr.New("too few arguments passed").ValidArgs("expected", "got") ErrTooManyArgsPassed = serr.New("too many arguments passed").ValidArgs("expected", "got") ErrAlreadyExists = serr.New("already exists").ValidArgs("arg_name", "value") ErrDoesNotExist = serr.New("does not exist").ValidArgs("arg_name", "value") ErrDoesNotValidate = serr.New("does not validate").ValidArgs("arg_name", "value") ErrEmptyStateNotSatisfied = serr.New("not satisfied").ValidArgs("arg_name", "value") // ErrTokenValueCannotBeEmpty omits the word "token" to display a message "value // cannot be empty" and omits in order to differentiate from a potential generic // `ErrValueCannotBeEmpty` error. ErrTokenValueCannotBeEmpty = serr.New("value cannot be empty").ValidArgs(string(ArgType), string(FlagType)) // ErrTokenValueMustBeEmpty omits the word "token" to display a message "value // must be empty" and omits in order to differentiate from a potential generic // `ErrValueMustBeEmpty` error. ErrTokenValueMustBeEmpty = serr.New("value must be empty").ValidArgs(string(ArgType), string(FlagType)) // ErrHelpSentinel is used to trigger help.Usage() in main() by just checking for err==nil when returned from an app.Main(). ErrHelpSentinel = serr.New(string(HelpArg)) )
var CmdHelp = AddCommandWithFunc(Token(HelpArg), ExecHelp). AddArg(Arg{ Name: CommandArg, Usage: "Specifies the command to show help for", Optional: true, Variadic: true, }.EmptyOk())
var MatchSpaces = regexp.MustCompile(`\s+`)
var RootCmd = NewCommand("", nil)
RootCmd is parent of all top level command and is where global flags will be attached.
var StderrWriter io.Writer = os.Stderr
var StdoutWriter io.Writer = os.Stderr
StderrWriter is the writer used to direct output to StdErr, but can be set to a bytes.Buffer to capture output during tests.
Functions ¶
func CheckExistence ¶
func CheckExistence[S ~[]RM, RM RequirementsMeeter](ctx Context, rms S) (err error)
func CommandString ¶
CommandString returns the full list of commands minus the flags
func EmptyStateSatisfied ¶
func EmptyStateSatisfied[S ~[]RM, RM RequirementsMeeter](ctx Context, tt TokenType, rms S) (err error)
func ExecHelp ¶
func ExecHelp(Context, *CommandInvoker) (err error)
ExecHelp "implements" the help command by simply delegating to help.Usage() by returning an sentinel error which help.Usage() will recognize and generate the appropriate output.
func ExecutableFilepath ¶
func MeetsRequirements ¶
func MeetsRequirements[S ~[]RM, RM RequirementsMeeter](ctx Context, tt TokenType, rms S) (err error)
func ValidateByFunc ¶
func ValidateByFunc[S ~[]RM, RM RequirementsMeeter](ctx Context, rms S) (err error)
func WarnOnError ¶
func WarnOnError(err error)
Types ¶
type Arg ¶
type Arg struct { Name ArgName Parent *Command Usage string Default interface{} Optional bool Variadic bool ExistsFunc func(Context, any, *Arg) error ValidateFunc func(Context, any, *Arg) error Type reflect.Kind SetValueFunc func(*Value) Value *Value Requires ArgRequires SuccessMsg string }
func (Arg) Check ¶
func (arg Arg) Check(requires ArgRequires) bool
func (Arg) CheckExistence ¶
func (Arg) ClearCheckFunc ¶
func (Arg) DefaultHelp ¶
func (Arg) EmptyStateSatisfied ¶
EmptyStateSatisfied ensures that values of .Requires for .Args are satisfied
func (Arg) IgnoreExists ¶
func (Arg) MustBeEmpty ¶
func (Arg) MustValidate ¶
func (Arg) SignatureHelp ¶
func (Arg) ValidateByFunc ¶
type ArgRequires ¶
type ArgRequires int
const ( MustExist ArgRequires = 1 << iota IgnoreExists NotExist EmptyOk MustBeEmpty NotEmpty MustValidate )
func AndRequires ¶
func AndRequires(requires ...ArgRequires) (ar ArgRequires)
func ArgEmptiness ¶
func ArgEmptiness(requires ArgRequires) ArgRequires
ArgEmptiness will return only one of the three emptiness modes, so it can be checked with case statement or simple equals (=).
func ArgExistence ¶
func ArgExistence(requires ArgRequires) ArgRequires
ArgExistence will return only one of the three existence modes, so it can be checked with case statement or simple equals (=).
func ArgValidation ¶
func ArgValidation(requires ArgRequires) ArgRequires
ArgValidation ArgEmptiness will return MustValidate if set, otherwise 0, so it can be checked with simple equals (=).
type Command ¶
type Command struct { Name Token Parent *Command ExecFunc ExecFunc Flags Flags Args Args SubCommands CommandMap // contains filtered or unexported fields }
func AddCommand ¶
func AddCommandWithFunc ¶
func InvokedCommand ¶
func NewCommand ¶
func (*Command) AddSubCommand ¶
func (*Command) DeclaredArgsCount ¶
DeclaredArgsCount returns the number of total args; required and optional
func (*Command) InvokedFlags ¶
InvokedFlags returns all the flags for th invoked command, including all parent flags including the root flags.
func (*Command) MeetsRequirements ¶
MeetsRequirements validates args and options passed on the CLI for full command path.
func (*Command) OptionalArgsCount ¶
OptionalArgsCount returns the number of optional args
func (*Command) ReceivedArgsCount ¶
ReceivedArgsCount returns number of args received on command line. Example: If the command is "make widget" and the os.Args has:
"/path/to/maker make widget -v -n foo bar baz"
then ReceivedArgsCount returns 3 for "foo bar baz."
func (*Command) RequiredArgsCount ¶
RequiredArgsCount returns the number of required args
func (*Command) SetFlagDefault ¶
func (*Command) SignatureHelp ¶
type CommandInvoker ¶
func Initialize ¶
func Initialize(ctx Context, params Params) (invoker *CommandInvoker, err error)
func NewCommandInvoker ¶
func NewCommandInvoker(params Params) *CommandInvoker
func (*CommandInvoker) ArgInt ¶
func (i *CommandInvoker) ArgInt(name ArgName) (n int)
func (*CommandInvoker) ArgString ¶
func (i *CommandInvoker) ArgString(name ArgName) (s string)
func (*CommandInvoker) ArgValue ¶
func (i *CommandInvoker) ArgValue(name ArgName) (value *Value)
func (*CommandInvoker) Args ¶
func (i *CommandInvoker) Args() Args
func (*CommandInvoker) InvokeCommand ¶
func (i *CommandInvoker) InvokeCommand(ctx Context) (err error)
func (*CommandInvoker) MeetsRequirements ¶
func (i *CommandInvoker) MeetsRequirements(ctx Context) (err error)
MeetsRequirements validates args and options passed on the CLI
func (*CommandInvoker) SubCommands ¶
func (i *CommandInvoker) SubCommands() CommandMap
type CommandType ¶
type CommandType int
const ( Undefined CommandType = iota RootCommand BranchCommand LeafCommand HelpCommand )
type ExecFunc ¶
type ExecFunc func(Context, *CommandInvoker) error
type Flags ¶
type Flags []Flag
func (Flags) DisplayWidth ¶
func (Flags) Initialize ¶
Initialize initializes the flag package flags by calling the flag package's flag.<Type>Var() function on a pointer to f.Arg.Value.<type> so that this flag 'f' will get the values passed on the command line, or the defaults if not passed.
func (Flags) SignatureHelp ¶
type Params ¶
type RequirementsMeeter ¶
type RequirementsMeeters ¶
type RequirementsMeeters []RequirementsMeeter
type Tokens ¶
type Tokens []Token
func (Tokens) Join ¶
Join joins a slice of Tokens into a strings. It is a copy of strings.Join() with small changes for tokens.