Documentation ¶
Overview ¶
Package gcmd provides console operations, like options/arguments reading and command running.
Index ¶
- Constants
- func BuildOptions(m map[string]string, prefix ...string) string
- func ContainsOpt(name string) bool
- func GetArg(index int, def ...string) *gvar.Var
- func GetArgAll() []string
- func GetOpt(name string, def ...string) *gvar.Var
- func GetOptAll() map[string]string
- func GetOptWithEnv(key string, def ...interface{}) *gvar.Var
- func Init(args ...string)
- func Scan(info ...interface{}) string
- func Scanf(format string, info ...interface{}) string
- type Command
- type FuncWithValue
- type Function
- type Option
- type Parser
- func (p *Parser) AutoRun() error
- func (p *Parser) BindHandle(cmd string, f func()) error
- func (p *Parser) BindHandleMap(m map[string]func()) error
- func (p *Parser) ContainsOpt(name string) bool
- func (p *Parser) GetArg(index int, def ...string) *gvar.Var
- func (p *Parser) GetArgAll() []string
- func (p *Parser) GetOpt(name string, def ...interface{}) *gvar.Var
- func (p *Parser) GetOptAll() map[string]string
- func (p *Parser) MarshalJSON() ([]byte, error)
- func (p *Parser) RunHandle(cmd string) error
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func BuildOptions ¶
BuildOptions builds the options as string.
func ContainsOpt ¶
ContainsOpt checks whether option named `name` exist in the arguments.
func GetArg ¶
GetArg returns the argument at `index` as gvar.Var.
func GetOpt ¶
GetOpt returns the option value named `name` as gvar.Var.
func GetOptWithEnv ¶
GetOptWithEnv returns the command line argument of the specified `key`. If the argument does not exist, then it returns the environment variable with specified `key`. It returns the default value `def` if none of them exists.
Fetching Rules: 1. Command line arguments are in lowercase format, eg: gf.`package name`.<variable name>; 2. Environment arguments are in uppercase format, eg: GF_`package name`_<variable name>;
Types ¶
type Command ¶
type Command struct { Name string // Command name(case-sensitive). Usage string // A brief line description about its usage, eg: gf build main.go [OPTION] Brief string // A brief info that describes what this command will do. Description string // A detailed description. Options []Option // Option array, configuring how this command act. Func Function // Custom function. FuncWithValue FuncWithValue // Custom function with output parameters that can interact with command caller. HelpFunc Function // Custom help function Examples string // Usage examples. Additional string // Additional info about this command, which will be appended to the end of help info. Strict bool // Strict parsing options, which means it returns error if invalid option given. // contains filtered or unexported fields }
Command holds the info about an argument that can handle custom logic.
func NewFromObject ¶
NewFromObject creates and returns a root command object using given object.
func (*Command) AddCommand ¶
AddCommand adds one or more sub-commands to current command.
func (*Command) AddObject ¶
AddObject adds one or more sub-commands to current command using struct object.
func (*Command) Print ¶
func (c *Command) Print()
Print prints help info to stdout for current command.
func (*Command) Run ¶
Run calls custom function that bound to this command.
type FuncWithValue ¶
FuncWithValue is similar like Func but with output parameters that can interact with command caller.
type Function ¶
Function is a custom command callback function that is bound to a certain argument.
type Option ¶
type Option struct { Name string // Option name. Short string // Option short. Brief string // Brief info about this Option, which is used in help info. Orphan bool // Whether this Option having or having no value bound to it. }
Option is the command value that is specified by a name or shor name. An Option can have or have no value bound to it.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser for arguments.
func Parse ¶
Parse creates and returns a new Parser with os.Args and supported options.
Note that the parameter `supportedOptions` is as [option name: need argument], which means the value item of `supportedOptions` indicates whether corresponding option name needs argument or not.
The optional parameter `strict` specifies whether stops parsing and returns error if invalid option passed.
func ParseWithArgs ¶
func ParseWithArgs(args []string, supportedOptions map[string]bool, strict ...bool) (*Parser, error)
ParseWithArgs creates and returns a new Parser with given arguments and supported options.
Note that the parameter `supportedOptions` is as [option name: need argument], which means the value item of `supportedOptions` indicates whether corresponding option name needs argument or not.
The optional parameter `strict` specifies whether stops parsing and returns error if invalid option passed.
func ParserFromCtx ¶
ParserFromCtx retrieves and returns Parser from context.
func (*Parser) AutoRun ¶
AutoRun automatically recognizes and executes the callback function by value of index 0 (the first console parameter).
func (*Parser) BindHandle ¶
BindHandle registers callback function `f` with `cmd`.
func (*Parser) BindHandleMap ¶
BindHandleMap registers callback function with map `m`.
func (*Parser) ContainsOpt ¶
ContainsOpt checks whether option named `name` exist in the arguments.
func (*Parser) GetArg ¶
GetArg returns the argument at `index` as gvar.Var.
func (*Parser) GetArgAll ¶
GetArgAll returns all parsed arguments.
func (*Parser) GetOpt ¶
GetOpt returns the option value named `name` as gvar.Var.
func (*Parser) GetOptAll ¶
GetOptAll returns all parsed options.
func (*Parser) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.