improve gcmd

This commit is contained in:
John
2019-09-16 20:57:43 +08:00
parent 1d7ded562c
commit 5d72a5b5ae
5 changed files with 87 additions and 9 deletions

View File

@ -1,5 +1,7 @@
# `v1.9.0`
该版本实际为`v2.0.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9.0`进行发布。
## 新特性
1. 增加`gf`命令行开发辅助工具https://goframe.org/toolchain/cli
@ -23,8 +25,8 @@
1. `Session`功能重构,新增`gsession`模块,`WebServer`默认使用文件存储`Session`https://goframe.org/net/ghttp/session
1. `WebServer`新增中间件特性并保留原有的HOOK设计两者都可实现请求拦截、预处理等等特性https://goframe.org/net/ghttp/router/middleware
1. `WebServer`新增更便捷的层级路由注册方式https://goframe.org/net/ghttp/group/level
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象
1. 新增`gdebug`模块,用于堆栈获取/打印;
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象https://goframe.org/os/gcmd/index
1. 新增`gdebug`模块,用于堆栈信息获取/打印;
## 重大调整

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright 2019 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,
@ -17,8 +17,9 @@ import (
)
var (
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
defaultCommandFuncMap = make(map[string]func())
)
// Custom initialization.
@ -29,7 +30,7 @@ func doInit() {
// Parsing os.Args with default algorithm.
// The option should use '=' to separate its name and value in default.
for _, arg := range os.Args {
array, _ := gregex.MatchString(`^\-{1,2}([\w\?]+)={0,1}(.*)$`, arg)
array, _ := gregex.MatchString(`^\-{1,2}([\w\?\.\-]+)={0,1}(.*)$`, arg)
if len(array) == 3 {
defaultParsedOptions[array[1]] = array[2]
} else {

58
os/gcmd/gcmd_handler.go Normal file
View File

@ -0,0 +1,58 @@
// Copyright 2019 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 (
"errors"
)
// BindHandle registers callback function <f> with <cmd>.
func BindHandle(cmd string, f func()) error {
if _, ok := defaultCommandFuncMap[cmd]; ok {
return errors.New("duplicated handle for command:" + cmd)
} else {
defaultCommandFuncMap[cmd] = f
}
return nil
}
// BindHandle registers callback function with map <m>.
func BindHandleMap(m map[string]func()) error {
var err error
for k, v := range m {
if err = BindHandle(k, v); err != nil {
return err
}
}
return err
}
// RunHandle executes the callback function registered by <cmd>.
func RunHandle(cmd string) error {
if handle, ok := defaultCommandFuncMap[cmd]; ok {
handle()
} else {
return errors.New("no handle found for command:" + cmd)
}
return nil
}
// AutoRun automatically recognizes and executes the callback function
// by value of index 0 (the first console parameter).
func AutoRun() error {
if cmd := GetArg(1); cmd != "" {
if handle, ok := defaultCommandFuncMap[cmd]; ok {
handle()
} else {
return errors.New("no handle found for command:" + cmd)
}
} else {
return errors.New("no command found")
}
return nil
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright 2019 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,
@ -21,6 +21,17 @@ func (p *Parser) BindHandle(cmd string, f func()) error {
return nil
}
// BindHandle registers callback function with map <m>.
func (p *Parser) BindHandleMap(m map[string]func()) error {
var err error
for k, v := range m {
if err = p.BindHandle(k, v); err != nil {
return err
}
}
return err
}
// RunHandle executes the callback function registered by <cmd>.
func (p *Parser) RunHandle(cmd string) error {
if handle, ok := p.commandFuncMap[cmd]; ok {

View File

@ -447,7 +447,10 @@ func Split(str, delimiter string) []string {
func SplitAndTrim(str, delimiter, cut string) []string {
array := strings.Split(str, delimiter)
for k, v := range array {
array[k] = strings.Trim(v, cut)
v = strings.Trim(v, cut)
if v != "" {
array[k] = strings.Trim(v, cut)
}
}
return array
}
@ -457,7 +460,10 @@ func SplitAndTrim(str, delimiter, cut string) []string {
func SplitAndTrimSpace(str, delimiter string) []string {
array := strings.Split(str, delimiter)
for k, v := range array {
array[k] = strings.TrimSpace(v)
v = strings.TrimSpace(v)
if v != "" {
array[k] = v
}
}
return array
}