Documentation
¶
Index ¶
- Constants
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- func BoolVar(p *bool, name string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func Float64(name string, value float64, usage string) *float64
- func Float64Var(p *float64, name string, value float64, usage string)
- func Int(name string, value int, usage string) *int
- func Int64(name string, value int64, usage string) *int64
- func Int64Var(p *int64, name string, value int64, usage string)
- func IntVar(p *int, name string, value int, usage string)
- func IsNonFlag(f *Flag) bool
- func LookupArgs(arguments []string, name string) (value string, found bool)
- func NArg() int
- func NFlag() int
- func NFormalNonFlag() int
- func NextArgs() []string
- func NonBool(index int, value bool, usage string) *bool
- func NonBoolVar(p *bool, index int, value bool, usage string)
- func NonDuration(index int, value time.Duration, usage string) *time.Duration
- func NonDurationVar(p *time.Duration, index int, value time.Duration, usage string)
- func NonFlagIndex(nonFlag *Flag) (int, bool)
- func NonFloat64(index int, value float64, usage string) *float64
- func NonFloat64Var(p *float64, index int, value float64, usage string)
- func NonInt(index int, value int, usage string) *int
- func NonInt64(index int, value int64, usage string) *int64
- func NonInt64Var(p *int64, index int, value int64, usage string)
- func NonIntVar(p *int, index int, value int, usage string)
- func NonString(index int, value string, usage string) *string
- func NonStringVar(p *string, index int, value string, usage string)
- func NonUint(index int, value uint, usage string) *uint
- func NonUint64(index int, value uint64, usage string) *uint64
- func NonUint64Var(p *uint64, index int, value uint64, usage string)
- func NonUintVar(p *uint, index int, value uint, usage string)
- func NonVar(value Value, index int, usage string)
- func NonVisit(fn func(*Flag))
- func NonVisitAll(fn func(*Flag))
- func Parse()
- func Parsed() bool
- func PrintDefaults()
- func Range(fn func(*Flag))
- func RangeAll(fn func(*Flag))
- func Set(name, value string) error
- func SplitArgs(arguments []string) (string, []string)
- func String(name string, value string, usage string) *string
- func StringVar(p *string, name string, value string, usage string)
- func StructVars(p interface{}) error
- func Uint(name string, value uint, usage string) *uint
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func UintVar(p *uint, name string, value uint, usage string)
- func UnquoteUsage(f *Flag) (name string, usage string)
- func Usage()
- func Var(value Value, name string, usage string)
- func Visit(fn func(*Flag))
- func VisitAll(fn func(*Flag))
- type Action
- type ActionCopier
- type ActionFunc
- type App
- func (a *App) Authors() []Author
- func (a *App) CmdName() string
- func (a *App) Compiled() time.Time
- func (a *App) Copyright() string
- func (a *App) Description() string
- func (a *App) Name() string
- func (a *App) SetAuthors(authors []Author) *App
- func (a *App) SetCmdName(cmdName string) *App
- func (a *App) SetCompiled(date time.Time) *App
- func (a *App) SetCopyright(copyright string) *App
- func (a *App) SetDescription(description string) *App
- func (a *App) SetName(appName string) *App
- func (a *App) SetNotFound(fn ActionFunc) *App
- func (a *App) SetScopeMatcher(fn func(cmdScope, execScope Scope) error) *App
- func (a *App) SetUsageTemplate(tmpl *template.Template) *App
- func (a *App) SetValidator(fn ValidateFunc) *App
- func (a *App) SetVersion(version string) *App
- func (a *App) UsageText(execScope ...Scope) string
- func (a *App) Version() string
- type Author
- type Command
- func (c *Command) AddFilter(filters ...Filter)
- func (c *Command) AddSubaction(cmdName, description string, action Action, scope ...Scope)
- func (c *Command) AddSubcommand(cmdName, description string, filters ...Filter) *Command
- func (c *Command) CmdName() string
- func (c *Command) Exec(ctx context.Context, arguments []string, execScope ...Scope) (stat *Status)
- func (c *Command) FindActionCommands(execScope ...Scope) []*Command
- func (c *Command) Flags() map[string]*Flag
- func (c *Command) GetMeta(key interface{}) interface{}
- func (c *Command) LookupSubcommand(pathCmdNames ...string) *Command
- func (c *Command) Parent() *Command
- func (c *Command) ParentVisible() bool
- func (c *Command) Path() (p []string)
- func (c *Command) PathString() string
- func (c *Command) Root() *Command
- func (c *Command) SetAction(action Action, scope ...Scope)
- func (c *Command) SetMeta(key interface{}, val interface{})
- func (c *Command) SetParentVisible(visible bool)
- func (c *Command) Subcommands() []*Command
- func (c *Command) UsageText(execScope ...Scope) string
- type Context
- func (c *Context) Args() []string
- func (c *Context) CheckStatus(err error, code int32, msg string, whenError ...func())
- func (c *Context) CmdPath() []string
- func (c *Context) CmdPathString() string
- func (c *Context) CmdScope() Scope
- func (c *Context) ExecScope() Scope
- func (c *Context) GetCmdMeta(key interface{}) interface{}
- func (c *Context) ThrowStatus(code int32, msg string, cause ...interface{})
- func (c *Context) UsageText() string
- type ErrorHandling
- type Filter
- type FilterCopier
- type FilterFunc
- type Flag
- type FlagSet
- func (f *FlagSet) ErrorHandling() ErrorHandling
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) NFormalNonFlag() int
- func (f *FlagSet) NextArgs() []string
- func (f *FlagSet) NonBool(index int, value bool, usage string) *bool
- func (f *FlagSet) NonBoolVar(p *bool, index int, value bool, usage string)
- func (f *FlagSet) NonDuration(index int, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) NonDurationVar(p *time.Duration, index int, value time.Duration, usage string)
- func (f *FlagSet) NonFloat64(index int, value float64, usage string) *float64
- func (f *FlagSet) NonFloat64Var(p *float64, index int, value float64, usage string)
- func (f *FlagSet) NonInt(index int, value int, usage string) *int
- func (f *FlagSet) NonInt64(index int, value int64, usage string) *int64
- func (f *FlagSet) NonInt64Var(p *int64, index int, value int64, usage string)
- func (f *FlagSet) NonIntVar(p *int, index int, value int, usage string)
- func (f *FlagSet) NonString(index int, value string, usage string) *string
- func (f *FlagSet) NonStringVar(p *string, index int, value string, usage string)
- func (f *FlagSet) NonUint(index int, value uint, usage string) *uint
- func (f *FlagSet) NonUint64(index int, value uint64, usage string) *uint64
- func (f *FlagSet) NonUint64Var(p *uint64, index int, value uint64, usage string)
- func (f *FlagSet) NonUintVar(p *uint, index int, value uint, usage string)
- func (f *FlagSet) NonVar(value Value, index int, usage string)
- func (f *FlagSet) NonVisit(fn func(*Flag))
- func (f *FlagSet) NonVisitAll(fn func(*Flag))
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) Range(fn func(*Flag))
- func (f *FlagSet) RangeAll(fn func(*Flag))
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) StructVars(p interface{}) error
- type Getter
- type Option
- type Scope
- type Status
- type ValidateFunc
- type Value
Examples ¶
Constants ¶
const ( StatusBadArgs int32 = 1 StatusNotFound int32 = 2 StatusParseFailed int32 = 3 StatusValidateFailed int32 = 4 StatusMismatchScope int32 = 5 )
Status code
Variables ¶
var ( // NewStatus creates a message status with code, msg and cause. // NOTE: // code=0 means no error // TYPE: // func NewStatus(code int32, msg string, cause interface{}) *Status NewStatus = status.New // NewStatusWithStack creates a message status with code, msg and cause and stack. // NOTE: // code=0 means no error // TYPE: // func NewStatusWithStack(code int32, msg string, cause interface{}) *Status NewStatusWithStack = status.NewWithStack // NewStatusFromQuery parses the query bytes to a status object. // TYPE: // func NewStatusFromQuery(b []byte, tagStack bool) *Status NewStatusFromQuery = status.FromQuery // CheckStatus if err!=nil, create a status with stack, and panic. // NOTE: // If err!=nil and msg=="", error text is set to msg // TYPE: // func Check(err error, code int32, msg string, whenError ...func()) CheckStatus = status.Check // ThrowStatus creates a status with stack, and panic. // TYPE: // func Throw(code int32, msg string, cause interface{}) ThrowStatus = status.Throw // CatchStatus recovers the panic and returns status. // NOTE: // Set `realStat` to true if a `Status` type is recovered // Example: // var stat *Status // defer CatchStatus(&stat) // TYPE: // func CatchStatus(statPtr **Status, realStat ...*bool) CatchStatus = status.CatchWithStack )
var CommandLine = NewFlagSet(os.Args[0], ExitOnError|ContinueOnUndefined)
CommandLine is the default set of command-line flags, parsed from os.Args. The top-level functions such as BoolVar, Arg, and so on are wrappers for the methods of CommandLine.
Functions ¶
func Arg ¶
Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist.
func Bool ¶
Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.
func BoolVar ¶
BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.
func Duration ¶
Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
func DurationVar ¶
DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration.
func Float64 ¶
Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.
func Float64Var ¶
Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.
func Int ¶
Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.
func Int64 ¶
Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.
func Int64Var ¶
Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.
func IntVar ¶
IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.
func LookupArgs ¶
LookupArgs lookups the value corresponding to the name directly from the arguments.
func NArg ¶
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func NFormalNonFlag ¶
func NFormalNonFlag() int
NFormalNonFlag returns the number of non-flag required in the definition.
func NonBool ¶
NonBool defines a bool non-flag with specified index, default value, and usage string. The return value is the address of a bool variable that stores the value of the non-flag.
func NonBoolVar ¶
NonBoolVar defines a bool non-flag with specified index, default value, and usage string. The argument p points to a bool variable in which to store the value of the non-flag.
func NonDuration ¶
NonDuration defines a time.Duration non with specified index, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the non-flag. The non-flag accepts a value acceptable to time.ParseDuration.
func NonDurationVar ¶
NonDurationVar defines a time.Duration non-flag with specified index, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the non-flag. The non-flag accepts a value acceptable to time.ParseDuration.
func NonFlagIndex ¶
NonFlagIndex gets the non-flag index from name.
func NonFloat64 ¶
NonFloat64 defines a float64 non-flag with specified index, default value, and usage string. The return value is the address of a float64 variable that stores the value of the non-flag.
func NonFloat64Var ¶
NonFloat64Var defines a float64 non-flag with specified index, default value, and usage string. The argument p points to a float64 variable in which to store the value of the non-flag.
func NonInt ¶
NonInt defines an int non-flag with specified index, default value, and usage string. The return value is the address of an int variable that stores the value of the non-flag.
func NonInt64 ¶
NonInt64 defines an int64 non-flag with specified index, default value, and usage string. The return value is the address of an int64 variable that stores the value of the non-flag.
func NonInt64Var ¶
NonInt64Var defines an int64 non-flag with specified index, default value, and usage string. The argument p points to an int64 variable in which to store the value of the non-flag.
func NonIntVar ¶
NonIntVar defines an int non-flag with specified index, default value, and usage string. The argument p points to an int variable in which to store the value of the non-flag.
func NonString ¶
NonString defines a string non-flag with specified index, default value, and usage string. The return value is the address of a string variable that stores the value of the non-flag.
func NonStringVar ¶
NonStringVar defines a string non-flag with specified index, default value, and usage string. The argument p points to a string variable in which to store the value of the non-flag.
func NonUint ¶
NonUint defines a uint non-flag with specified index, default value, and usage string. The return value is the address of a uint variable that stores the value of the non-flag.
func NonUint64 ¶
NonUint64 defines a uint64 non-flag with specified index, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the non-flag.
func NonUint64Var ¶
NonUint64Var defines a uint64 non-flag with specified index, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the non-flag.
func NonUintVar ¶
NonUintVar defines a uint non-flag with specified index, default value, and usage string. The argument p points to a uint variable in which to store the value of the non-flag.
func NonVisit ¶
func NonVisit(fn func(*Flag))
NonVisit visits the command-line non-flags in lexicographical order, calling fn for each. It visits only those non-flags that have been set.
func NonVisitAll ¶
func NonVisitAll(fn func(*Flag))
NonVisitAll visits the command-line non-flags in lexicographical order, calling fn for each. It visits all non-flags, even those not set.
func Parse ¶
func Parse()
Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined command-line flags. For an integer valued flag x, the default output has the form
-x int usage-message-for-x (default 7)
The usage message will appear on a separate line for anything but a bool flag with a one-byte name. For bool flags, the type is omitted and if the flag name is one byte the usage message appears on the same line. The parenthetical default is omitted if the default is the zero value for the type. The listed type, here int, can be changed by placing a back-quoted name in the flag's usage string; the first such item in the message is taken to be a parameter name to show in the message and the back quotes are stripped from the message when displayed. For instance, given
flag.String("I", "", "search `directory` for include files")
the output will be
-I directory search directory for include files.
To change the destination for flag messages, call CommandLine.SetOutput.
func Range ¶
func Range(fn func(*Flag))
Range visits the command-line flags and non-flags in lexicographical order, calling fn for each. It visits only those flags and non-flags that have been set.
func RangeAll ¶
func RangeAll(fn func(*Flag))
RangeAll visits the command-line flags and non-flags in lexicographical order, calling fn for each. It visits all flags and non-flags, even those not set.
func String ¶
String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.
func StringVar ¶
StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.
func StructVars ¶
func StructVars(p interface{}) error
StructVars defines flags based on struct tags and binds to fields. NOTE:
Not support nested fields
Example ¶
os.Args = []string{
"go test",
"-test.timeout", "30s",
"-test.v",
"-test.count", "1",
"-test.run", "^(TestStructVars)$",
"flag_test.go",
}
type Args struct {
Run string `flag:"test.run; def=.*; usage=function name pattern"`
Timeout time.Duration `flag:"test.timeout"`
V bool `flag:"test.v"`
X int `flag:"def=10"`
Y string `flag:"?0"` // the first non-flag
}
var args Args
err := StructVars(&args)
if err != nil {
panic(err)
}
Parse()
fmt.Printf("%+v\n", args)
Output: {Run:^(TestStructVars)$ Timeout:30s V:true X:10 Y:flag_test.go}
func Uint ¶
Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.
func Uint64 ¶
Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64Var ¶
Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.
func UintVar ¶
UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for a flag and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the flag's value, or the empty string if the flag is boolean.
func Var ¶
Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Types ¶
type Action ¶
type Action interface {
// Execute executes action.
// NOTE:
// If need to return an error, use *Context.ThrowStatus or *Context.CheckStatus
Execute(*Context)
}
Action action of action
type ActionCopier ¶
type ActionCopier interface {
DeepCopy() Action
}
ActionCopier an interface that can create its own copy
type ActionFunc ¶
type ActionFunc func(*Context)
ActionFunc action function NOTE:
If need to return an error, use *Context.ThrowStatus or *Context.CheckStatus
func (ActionFunc) Execute ¶
func (fn ActionFunc) Execute(c *Context)
Execute implements Action interface.
type App ¶
type App struct {
*Command
// contains filtered or unexported fields
}
App is a application structure. It is recommended that an app be created with the flagx.NewApp() function
Example ¶
app := NewApp()
app.SetCmdName("testapp")
app.SetDescription("this is a app for testing")
app.SetAuthors([]Author{{
Name: "henrylee2cn",
Email: "henrylee2cn@gmail.com",
}})
app.SetValidator(func(v interface{}) error {
return vd.Validate(v)
})
app.AddFilter(new(Filter1))
// cmd: testapp a
app.AddSubaction("a", "subcommand a", new(Action1))
b := app.AddSubcommand("b", "subcommand b", FilterFunc(Filter2))
{
// cmd: testapp b c
b.AddSubaction("c", "subcommand c", new(Action2))
// cmd: testapp b d
b.AddSubaction("d", "subcommand d", ActionFunc(Action3))
}
app.SetNotFound(func(c *Context) {
fmt.Printf("NotFound: cmd=%q, uasge=%s\n", c.CmdPathString(), c.UsageText())
})
fmt.Println(app.UsageText())
// test: testapp
// not found
stat := app.Exec(context.TODO(), []string{"-g=flagx", "false"})
if !stat.OK() {
panic(stat)
}
// test: testapp a
stat = app.Exec(context.TODO(), []string{"-g=henry", "true", "a", "-id", "1", "~/m/n"})
if !stat.OK() {
panic(stat)
}
// test: testapp b
stat = app.Exec(context.TODO(), []string{"-g=flagx", "false", "b"})
if !stat.OK() {
panic(stat)
}
// test: testapp b c
// not found
stat = app.Exec(context.TODO(), []string{"-g=flagx", "false", "b", "c", "name=henry"})
if !stat.OK() {
panic(stat)
}
// test: testapp b d
stat = app.Exec(context.TODO(), []string{"-g=flagx", "false", "b", "d"})
if !stat.OK() {
panic(stat)
}
Output: testapp - v0.0.1 this is a app for testing USAGE: -g string global param g ?0 bool param view $testapp a subcommand a -id int param id ?0 string param path $testapp b ... subcommand b $testapp b c subcommand c -name string param name $testapp b d subcommand d AUTHOR: henrylee2cn <henrylee2cn@gmail.com> NotFound: cmd="testapp", uasge=-g string global param g ?0 bool param view $testapp a subcommand a -id int param id ?0 string param path $testapp b ... subcommand b $testapp b c subcommand c -name string param name $testapp b d subcommand d Filter1 start: args=[-g=henry true a -id 1 ~/m/n], G=henry Action1: args=[-g=henry true a -id 1 ~/m/n], path="testapp a", object=&{ID:1 Path:~/m/n} Filter1 end: args=[-g=henry true a -id 1 ~/m/n] NotFound: cmd="testapp b", uasge=$testapp b ... subcommand b $testapp b c subcommand c -name string param name $testapp b d subcommand d Filter1 start: args=[-g=flagx false b c name=henry], V=false Filter2 start: args=[-g=flagx false b c name=henry], start at=2020-02-13 13:48:15 +0800 CST Action2: args=[-g=flagx false b c name=henry], path="testapp b c", object=&{Name:} Filter2 end: args=[-g=flagx false b c name=henry], cost time=1µs Filter1 end: args=[-g=flagx false b c name=henry] Filter1 start: args=[-g=flagx false b d], V=false Filter2 start: args=[-g=flagx false b d], start at=2020-02-13 13:48:15 +0800 CST Action3: args=[-g=flagx false b d], path="testapp b d" Filter2 end: args=[-g=flagx false b d], cost time=1µs Filter1 end: args=[-g=flagx false b d]
func (*App) CmdName ¶
CmdName returns the command name of the application. Defaults to filepath.Base(os.Args[0])
func (*App) Description ¶
Description returns description the of the application.
func (*App) SetAuthors ¶
SetAuthors sets the list of all authors who contributed.
func (*App) SetCmdName ¶
SetCmdName sets the command name of the application. NOTE:
remove '-' prefix automatically
func (*App) SetCompiled ¶
SetCompiled sets the compilation date.
func (*App) SetCopyright ¶
SetCopyright sets copyright of the binary if any.
func (*App) SetDescription ¶
SetDescription sets description the of the application.
func (*App) SetNotFound ¶
func (a *App) SetNotFound(fn ActionFunc) *App
SetNotFound sets the action when the correct command cannot be found.
func (*App) SetScopeMatcher ¶
SetScopeMatcher sets the scope matching function.
func (*App) SetUsageTemplate ¶
SetUsageTemplate sets usage template.
func (*App) SetValidator ¶
func (a *App) SetValidator(fn ValidateFunc) *App
SetValidator sets parameter validator for struct action and struct filter.
func (*App) SetVersion ¶
SetVersion sets the version of the application.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command a command object
func (*Command) AddFilter ¶
AddFilter adds the filter action. NOTE:
if filter is a struct, it can implement the copier interface; panic when something goes wrong
func (*Command) AddSubaction ¶
AddSubaction adds a subcommand and its action. NOTE:
panic when something goes wrong
func (*Command) AddSubcommand ¶
AddSubcommand adds a subcommand. NOTE:
panic when something goes wrong
func (*Command) Exec ¶
Exec executes the command. NOTE:
@arguments does not contain the command name; the default value of @scope is 0.
func (*Command) FindActionCommands ¶
FindActionCommands finds list of action commands by the executor scope. NOTE:
if @scopes is empty, all action commands are returned.
func (*Command) GetMeta ¶
func (c *Command) GetMeta(key interface{}) interface{}
GetMeta gets the command meta.
func (*Command) LookupSubcommand ¶
LookupSubcommand lookups subcommand by path names. NOTE:
returns nil if it does not exist.
func (*Command) ParentVisible ¶
ParentVisible returns the visibility in parent command usage.
func (*Command) PathString ¶
PathString returns the command path string.
func (*Command) SetAction ¶
SetAction sets the action of the command. NOTE:
if action is a struct, it can implement the copier interface; panic when something goes wrong.
func (*Command) SetMeta ¶
func (c *Command) SetMeta(key interface{}, val interface{})
SetMeta sets the command meta.
func (*Command) SetParentVisible ¶
SetParentVisible sets the visibility in parent command usage.
func (*Command) Subcommands ¶
Subcommands returns the subcommands.
type Context ¶
Context context of an action execution
func (*Context) CheckStatus ¶
CheckStatus if err!=nil, create a status with stack, and panic. NOTE:
If err!=nil and msg=="", error text is set to msg
func (*Context) CmdPathString ¶
CmdPathString returns the command path string.
func (*Context) GetCmdMeta ¶
func (c *Context) GetCmdMeta(key interface{}) interface{}
GetCmdMeta gets the command meta.
func (*Context) ThrowStatus ¶
ThrowStatus creates a status with stack, and panic.
type ErrorHandling ¶
type ErrorHandling = flag.ErrorHandling
ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
const ( ContinueOnError ErrorHandling = flag.ContinueOnError // Return a descriptive error. ExitOnError ErrorHandling = flag.ExitOnError // Call os.Exit(2). PanicOnError ErrorHandling = flag.PanicOnError // Call panic with a descriptive error. ContinueOnUndefined ErrorHandling = 1 << 30 // Ignore provided but undefined flags )
These constants cause FlagSet.Parse to behave as described if the parse fails.
type Filter ¶
type Filter interface {
Filter(c *Context, next ActionFunc)
}
Filter global options of app NOTE:
If need to return an error, use *Context.ThrowStatus or *Context.CheckStatus
type FilterCopier ¶
type FilterCopier interface {
DeepCopy() Filter
}
FilterCopier an interface that can create its own copy
type FilterFunc ¶
type FilterFunc func(c *Context, next ActionFunc)
FilterFunc filter function NOTE:
If need to return an error, use *Context.ThrowStatus or *Context.CheckStatus
func (FilterFunc) Filter ¶
func (fn FilterFunc) Filter(c *Context, next ActionFunc)
Filter implements Filter interface.
type FlagSet ¶
A FlagSet represents a set of defined flags. The zero value of a FlagSet has no name and has ContinueOnError error handling.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
NewFlagSet returns a new, empty flag set with the specified name and error handling property. If the name is not empty, it will be printed in the default usage message and in error messages.
func (*FlagSet) ErrorHandling ¶
func (f *FlagSet) ErrorHandling() ErrorHandling
ErrorHandling returns the error handling behavior of the flag set.
func (*FlagSet) Init ¶
func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
Init sets the name and error handling property for a flag set. By default, the zero FlagSet uses an empty name and the ContinueOnError error handling policy.
func (*FlagSet) Lookup ¶
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) NFormalNonFlag ¶
NFormalNonFlag returns the number of non-flag required in the definition.
func (*FlagSet) NonBool ¶
NonBool defines a bool non-flag with specified index, default value, and usage string. The return value is the address of a bool variable that stores the value of the non-flag.
func (*FlagSet) NonBoolVar ¶
NonBoolVar defines a bool non-flag with specified index, default value, and usage string. The argument p points to a bool variable in which to store the value of the non-flag.
func (*FlagSet) NonDuration ¶
NonDuration defines a time.Duration non-flag with specified index, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the non-flag. The non-flag accepts a value acceptable to time.ParseDuration.
func (*FlagSet) NonDurationVar ¶
NonDurationVar defines a time.Duration non-flag with specified index, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the non-flag. The non-flag accepts a value acceptable to time.ParseDuration.
func (*FlagSet) NonFloat64 ¶
NonFloat64 defines a float64 non-flag with specified index, default value, and usage string. The return value is the address of a float64 variable that stores the value of the non-flag.
func (*FlagSet) NonFloat64Var ¶
NonFloat64Var defines a float64 non-flag with specified index, default value, and usage string. The argument p points to a float64 variable in which to store the value of the non-flag.
func (*FlagSet) NonInt ¶
NonInt defines an int non-flag with specified index, default value, and usage string. The return value is the address of an int variable that stores the value of the non-flag.
func (*FlagSet) NonInt64 ¶
NonInt64 defines an int64 non-flag with specified index, default value, and usage string. The return value is the address of an int64 variable that stores the value of the non-flag.
func (*FlagSet) NonInt64Var ¶
NonInt64Var defines an int64 non-flag with specified index, default value, and usage string. The argument p points to an int64 variable in which to store the value of the non-flag.
func (*FlagSet) NonIntVar ¶
NonIntVar defines an int non-flag with specified index, default value, and usage string. The argument p points to an int variable in which to store the value of the non-flag.
func (*FlagSet) NonString ¶
NonString defines a string non-flag with specified index, default value, and usage string. The return value is the address of a string variable that stores the value of the non-flag.
func (*FlagSet) NonStringVar ¶
NonStringVar defines a string non-flag with specified index, default value, and usage string. The argument p points to a string variable in which to store the value of the non-flag.
func (*FlagSet) NonUint ¶
NonUint defines a uint non-flag with specified index, default value, and usage string. The return value is the address of a uint variable that stores the value of the non-flag.
func (*FlagSet) NonUint64 ¶
NonUint64 defines a uint64 non-flag with specified index, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the non-flag.
func (*FlagSet) NonUint64Var ¶
NonUint64Var defines a uint64 non-flag with specified index, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the non-flag.
func (*FlagSet) NonUintVar ¶
NonUintVar defines a uint non-flag with specified index, default value, and usage string. The argument p points to a uint variable in which to store the value of the non-flag.
func (*FlagSet) NonVisit ¶
NonVisit visits the non-flags in lexicographical order, calling fn for each. It visits only those non-flags that have been set.
func (*FlagSet) NonVisitAll ¶
NonVisitAll visits the non-flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
func (*FlagSet) Parse ¶
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help or -h were set but not defined.
func (*FlagSet) PrintDefaults ¶
func (f *FlagSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set. See the documentation for the global function PrintDefaults for more information.
func (*FlagSet) Range ¶
Range visits the flags and non-flags in lexicographical order, calling fn for each. It visits only those flags and non-flags that have been set.
func (*FlagSet) RangeAll ¶
RangeAll visits the flags and non-flags in lexicographical order, calling fn for each. It visits all flags and non-flags, even those not set.
func (*FlagSet) StructVars ¶
StructVars defines flags based on struct tags and binds to fields. NOTE:
Not support nested fields
type Getter ¶
Getter is an interface that allows the contents of a Value to be retrieved. It wraps the Value interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All Value types provided by this package satisfy the Getter interface.
type Option ¶
Option command option
func LookupOptions ¶
LookupOptions lookups the options corresponding to the name directly from the arguments.
type Scope ¶
type Scope int32
Scope command scope
const ( // InitialScope the default scope InitialScope Scope = 0 )
type ValidateFunc ¶
type ValidateFunc func(interface{}) error
ValidateFunc validator for struct flag
type Value ¶
Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)
If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.
Set is called once, in command line order, for each flag present. The flag package may call the String method with a zero-valued receiver, such as a nil pointer.