gcmd

package
v1.16.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 11 Imported by: 50

Documentation

Overview

Package gcmd provides console operations, like options/arguments reading and command running.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoRun added in v1.9.1

func AutoRun() error

AutoRun automatically recognizes and executes the callback function by value of index 0 (the first console parameter).

func BindHandle added in v1.9.1

func BindHandle(cmd string, f func()) error

BindHandle registers callback function <f> with <cmd>.

func BindHandleMap added in v1.9.1

func BindHandleMap(m map[string]func()) error

BindHandleMap registers callback function with map <m>.

func BuildOptions

func BuildOptions(m map[string]string, prefix ...string) string

BuildOptions builds the options as string.

func ContainsOpt

func ContainsOpt(name string) bool

ContainsOpt checks whether option named <name> exist in the arguments.

func GetArg

func GetArg(index int, def ...string) string

GetArg returns the argument at <index>.

func GetArgAll

func GetArgAll() []string

GetArgAll returns all parsed arguments.

func GetArgVar

func GetArgVar(index int, def ...string) *gvar.Var

GetArgVar returns the argument at <index> as gvar.Var.

func GetOpt

func GetOpt(name string, def ...string) string

GetOpt returns the option value named <name>.

func GetOptAll

func GetOptAll() map[string]string

GetOptAll returns all parsed options.

func GetOptVar

func GetOptVar(name string, def ...string) *gvar.Var

GetOptVar returns the option value named <name> as gvar.Var.

func GetOptWithEnv added in v1.15.4

func GetOptWithEnv(key string, def ...interface{}) *gvar.Var

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>;

func GetWithEnv added in v1.14.6

func GetWithEnv(key string, def ...interface{}) *gvar.Var

GetWithEnv is alias of GetOptWithEnv. Deprecated, use GetOptWithEnv instead.

func Init added in v1.14.6

func Init(args ...string)

Custom initialization.

func RunHandle added in v1.9.1

func RunHandle(cmd string) error

RunHandle executes the callback function registered by <cmd>.

func Scan

func Scan(info ...interface{}) string

Scan prints <info> to stdout, reads and returns user input, which stops by '\n'.

func Scanf

func Scanf(format string, info ...interface{}) string

Scanf prints <info> to stdout with <format>, reads and returns user input, which stops by '\n'.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser for arguments.

func Parse

func Parse(supportedOptions map[string]bool, strict ...bool) (*Parser, error)

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

func (p *Parser) AutoRun() error

AutoRun automatically recognizes and executes the callback function by value of index 0 (the first console parameter).

func (*Parser) BindHandle

func (p *Parser) BindHandle(cmd string, f func()) error

BindHandle registers callback function <f> with <cmd>.

func (*Parser) BindHandleMap added in v1.9.1

func (p *Parser) BindHandleMap(m map[string]func()) error

BindHandleMap registers callback function with map <m>.

func (*Parser) ContainsOpt

func (p *Parser) ContainsOpt(name string) bool

ContainsOpt checks whether option named <name> exist in the arguments.

func (*Parser) GetArg

func (p *Parser) GetArg(index int, def ...string) string

GetArg returns the argument at <index>.

func (*Parser) GetArgAll

func (p *Parser) GetArgAll() []string

GetArgAll returns all parsed arguments.

func (*Parser) GetArgVar

func (p *Parser) GetArgVar(index int, def ...string) *gvar.Var

GetArgVar returns the argument at <index> as gvar.Var.

func (*Parser) GetOpt

func (p *Parser) GetOpt(name string, def ...string) string

GetOpt returns the option value named <name>.

func (*Parser) GetOptAll

func (p *Parser) GetOptAll() map[string]string

GetOptAll returns all parsed options.

func (*Parser) GetOptVar

func (p *Parser) GetOptVar(name string, def ...interface{}) *gvar.Var

GetOptVar returns the option value named <name> as gvar.Var.

func (*Parser) MarshalJSON

func (p *Parser) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*Parser) RunHandle

func (p *Parser) RunHandle(cmd string) error

RunHandle executes the callback function registered by <cmd>.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL