2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf.
|
2017-12-30 23:49:55 +08:00
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// Package gcmd provides console operations, like options/values reading and command running.
|
2019-01-16 13:35:16 +08:00
|
|
|
|
//
|
2018-03-29 13:46:05 +08:00
|
|
|
|
// 命令行管理.
|
2017-12-30 23:49:55 +08:00
|
|
|
|
package gcmd
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"errors"
|
2018-09-13 18:40:04 +08:00
|
|
|
|
"regexp"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 命令行参数列表
|
2017-12-30 23:49:55 +08:00
|
|
|
|
type gCmdValue struct {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
values []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 命令行选项列表
|
2017-12-30 23:49:55 +08:00
|
|
|
|
type gCmdOption struct {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
options map[string]string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 终端管理对象(全局)
|
2017-12-30 23:49:55 +08:00
|
|
|
|
var Value = &gCmdValue{} // 终端参数-命令参数列表
|
|
|
|
|
var Option = &gCmdOption{} // 终端参数-选项参数列表
|
2017-11-23 10:21:28 +08:00
|
|
|
|
var cmdFuncMap = make(map[string]func()) // 终端命令及函数地址对应表
|
|
|
|
|
|
|
|
|
|
// 检查并初始化console参数,在包加载的时候触发
|
|
|
|
|
// 初始化时执行,不影响运行时性能
|
|
|
|
|
func init() {
|
2018-09-13 18:40:04 +08:00
|
|
|
|
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
Option.options = make(map[string]string)
|
|
|
|
|
for i := 0; i < len(os.Args); i++ {
|
|
|
|
|
result := reg.FindStringSubmatch(os.Args[i])
|
|
|
|
|
if len(result) > 1 {
|
|
|
|
|
Option.options[result[1]] = result[2]
|
|
|
|
|
} else {
|
|
|
|
|
Value.values = append(Value.values, os.Args[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回所有的命令行参数values
|
2017-12-30 23:49:55 +08:00
|
|
|
|
func (c *gCmdValue) GetAll() []string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return c.values
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回所有的命令行参数options
|
2017-12-30 23:49:55 +08:00
|
|
|
|
func (c *gCmdOption) GetAll() map[string]string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return c.options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得一条指定索引位置的value参数
|
2018-09-13 18:40:04 +08:00
|
|
|
|
func (c *gCmdValue) Get(index uint8, def...string) string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if index < uint8(len(c.values)) {
|
|
|
|
|
return c.values[index]
|
2018-09-13 18:40:04 +08:00
|
|
|
|
} else if len(def) > 0 {
|
|
|
|
|
return def[0]
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-30 23:49:55 +08:00
|
|
|
|
// 获得一条指定索引位置的option参数;
|
2018-09-13 18:40:04 +08:00
|
|
|
|
func (c *gCmdOption) Get(key string, def...string) string {
|
2017-12-30 23:49:55 +08:00
|
|
|
|
if option, ok := c.options[key]; ok {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return option
|
2018-09-13 18:40:04 +08:00
|
|
|
|
} else if len(def) > 0 {
|
|
|
|
|
return def[0]
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 13:03:29 +08:00
|
|
|
|
// 绑定命令行参数及对应的命令函数,注意命令函数参数是函数的内存地址
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 如果操作失败返回错误信息
|
|
|
|
|
func BindHandle (cmd string, f func()) error {
|
2017-12-30 23:49:55 +08:00
|
|
|
|
if _, ok := cmdFuncMap[cmd]; ok {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return errors.New("duplicated handle for command:" + cmd)
|
|
|
|
|
} else {
|
|
|
|
|
cmdFuncMap[cmd] = f
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行命令对应的函数
|
|
|
|
|
func RunHandle (cmd string) error {
|
2017-12-30 23:49:55 +08:00
|
|
|
|
if handle, ok := cmdFuncMap[cmd]; ok {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
handle()
|
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
return errors.New("no handle found for command:" + cmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自动识别命令参数并执行命令参数对应的函数
|
|
|
|
|
func AutoRun () error {
|
2017-12-30 23:49:55 +08:00
|
|
|
|
if cmd := Value.Get(1); cmd != "" {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if handle, ok := cmdFuncMap[cmd]; ok {
|
|
|
|
|
handle()
|
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
return errors.New("no handle found for command:" + cmd)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return errors.New("no command found")
|
|
|
|
|
}
|
|
|
|
|
}
|