mirror of
https://gitee.com/johng/gf
synced 2026-07-08 14:39:50 +08:00
gcmd updates
This commit is contained in:
@ -6,33 +6,28 @@
|
||||
//
|
||||
|
||||
// Package gcmd provides console operations, like options/values reading and command running.
|
||||
//
|
||||
// 命令行管理.
|
||||
package gcmd
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"os"
|
||||
"errors"
|
||||
"regexp"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// 命令行参数列表
|
||||
// Console values.
|
||||
type gCmdValue struct {
|
||||
values []string
|
||||
}
|
||||
|
||||
// 命令行选项列表
|
||||
// Console options.
|
||||
type gCmdOption struct {
|
||||
options map[string]string
|
||||
}
|
||||
|
||||
// 终端管理对象(全局)
|
||||
var Value = &gCmdValue{} // 终端参数-命令参数列表
|
||||
var Option = &gCmdOption{} // 终端参数-选项参数列表
|
||||
var cmdFuncMap = make(map[string]func()) // 终端命令及函数地址对应表
|
||||
var Value = &gCmdValue{} // Console values.
|
||||
var Option = &gCmdOption{} // Console options.
|
||||
var cmdFuncMap = make(map[string]func()) // Registered callback functions.
|
||||
|
||||
// 检查并初始化console参数,在包加载的时候触发
|
||||
// 初始化时执行,不影响运行时性能
|
||||
func init() {
|
||||
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
|
||||
Option.options = make(map[string]string)
|
||||
@ -46,67 +41,34 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// 返回所有的命令行参数values
|
||||
func (c *gCmdValue) GetAll() []string {
|
||||
return c.values
|
||||
}
|
||||
|
||||
// 返回所有的命令行参数options
|
||||
func (c *gCmdOption) GetAll() map[string]string {
|
||||
return c.options
|
||||
}
|
||||
|
||||
// 获得一条指定索引位置的value参数
|
||||
func (c *gCmdValue) Get(index uint8, def...string) string {
|
||||
if index < uint8(len(c.values)) {
|
||||
return c.values[index]
|
||||
} else if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获得一条指定索引位置的option参数;
|
||||
func (c *gCmdOption) Get(key string, def...string) string {
|
||||
if option, ok := c.options[key]; ok {
|
||||
return option
|
||||
} else if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 绑定命令行参数及对应的命令函数,注意命令函数参数是函数的内存地址
|
||||
// 如果操作失败返回错误信息
|
||||
func BindHandle (cmd string, f func()) error {
|
||||
// BindHandle registers callback function <f> with <cmd>.
|
||||
func BindHandle (cmd string, f func()) {
|
||||
if _, ok := cmdFuncMap[cmd]; ok {
|
||||
return errors.New("duplicated handle for command:" + cmd)
|
||||
glog.Fatal("duplicated handle for command:" + cmd)
|
||||
} else {
|
||||
cmdFuncMap[cmd] = f
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 执行命令对应的函数
|
||||
func RunHandle (cmd string) error {
|
||||
// RunHandle executes the callback function registered by <cmd>.
|
||||
func RunHandle (cmd string) {
|
||||
if handle, ok := cmdFuncMap[cmd]; ok {
|
||||
handle()
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("no handle found for command:" + cmd)
|
||||
glog.Fatal("no handle found for command:" + cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// 自动识别命令参数并执行命令参数对应的函数
|
||||
func AutoRun () error {
|
||||
// AutoRun automatically recognizes and executes the callback function
|
||||
// by value of index 0 (the first console parameter).
|
||||
func AutoRun () {
|
||||
if cmd := Value.Get(1); cmd != "" {
|
||||
if handle, ok := cmdFuncMap[cmd]; ok {
|
||||
handle()
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("no handle found for command:" + cmd)
|
||||
glog.Fatal("no handle found for command:" + cmd)
|
||||
}
|
||||
} else {
|
||||
return errors.New("no command found")
|
||||
glog.Fatal("no command found")
|
||||
}
|
||||
}
|
||||
|
||||
32
g/os/gcmd/gcmd_option.go
Normal file
32
g/os/gcmd/gcmd_option.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
//
|
||||
|
||||
package gcmd
|
||||
|
||||
import "github.com/gogf/gf/g/container/gvar"
|
||||
|
||||
// GetAll returns all option values as map[string]string.
|
||||
func (c *gCmdOption) GetAll() map[string]string {
|
||||
return c.options
|
||||
}
|
||||
|
||||
// Get returns the option value string specified by <key>,
|
||||
// if value dose not exist, then returns <def>.
|
||||
func (c *gCmdOption) Get(key string, def...string) string {
|
||||
if option, ok := c.options[key]; ok {
|
||||
return option
|
||||
} else if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetVar returns the option value as gvar.VarRead object specified by <key>,
|
||||
// if value does not exist, then returns <def> as its default value.
|
||||
func (c *gCmdOption) GetVar(key string, def...string) gvar.VarRead {
|
||||
return gvar.NewRead(c.Get(key, def...), true)
|
||||
}
|
||||
32
g/os/gcmd/gcmd_value.go
Normal file
32
g/os/gcmd/gcmd_value.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
//
|
||||
|
||||
package gcmd
|
||||
|
||||
import "github.com/gogf/gf/g/container/gvar"
|
||||
|
||||
// GetAll returns all values as a slice of string.
|
||||
func (c *gCmdValue) GetAll() []string {
|
||||
return c.values
|
||||
}
|
||||
|
||||
// Get returns value by index <index> as string,
|
||||
// if value does not exist, then returns <def>.
|
||||
func (c *gCmdValue) Get(index int, def...string) string {
|
||||
if index < len(c.values) {
|
||||
return c.values[index]
|
||||
} else if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetVar returns value by index <index> as gvar.VarRead object,
|
||||
// if value does not exist, then returns <def> as its default value.
|
||||
func (c *gCmdValue) GetVar(index int, def...string) gvar.VarRead {
|
||||
return gvar.NewRead(c.Get(index, def...), true)
|
||||
}
|
||||
Reference in New Issue
Block a user