Documentation ¶
Overview ¶
Package gcmd provides console operations, like options/arguments reading and command running.
Index ¶
- func AutoRun() error
- func BindHandle(cmd string, f func()) error
- func BindHandleMap(m map[string]func()) error
- 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 RunHandle(cmd string) error
- func Scan(info ...interface{}) string
- func Scanf(format string, info ...interface{}) string
- 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 ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoRun ¶
func AutoRun() error
AutoRun automatically recognizes and executes the callback function by value of index 0 (the first console parameter).
func BindHandle ¶
BindHandle registers callback function `f` with `cmd`.
func BindHandleMap ¶
BindHandleMap registers callback function with map `m`.
func BuildOptions ¶
BuildOptions builds the options as string.
func ContainsOpt ¶
ContainsOpt checks whether option named `name` exist in the arguments.
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 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 (*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) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.