From 5d72a5b5aedb946e337bae72094867c17d06152f Mon Sep 17 00:00:00 2001 From: John Date: Mon, 16 Sep 2019 20:57:43 +0800 Subject: [PATCH] improve gcmd --- RELEASE.2.MD | 6 ++-- os/gcmd/gcmd.go | 9 +++--- os/gcmd/gcmd_handler.go | 58 ++++++++++++++++++++++++++++++++++ os/gcmd/gcmd_parser_handler.go | 13 +++++++- text/gstr/gstr.go | 10 ++++-- 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 os/gcmd/gcmd_handler.go diff --git a/RELEASE.2.MD b/RELEASE.2.MD index 55e506fff..96b5e027b 100644 --- a/RELEASE.2.MD +++ b/RELEASE.2.MD @@ -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`模块,用于堆栈信息获取/打印; ## 重大调整 diff --git a/os/gcmd/gcmd.go b/os/gcmd/gcmd.go index b8bdaafaf..b40d8483b 100644 --- a/os/gcmd/gcmd.go +++ b/os/gcmd/gcmd.go @@ -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 { diff --git a/os/gcmd/gcmd_handler.go b/os/gcmd/gcmd_handler.go new file mode 100644 index 000000000..1fa5096d2 --- /dev/null +++ b/os/gcmd/gcmd_handler.go @@ -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 with . +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 . +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 . +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 +} diff --git a/os/gcmd/gcmd_parser_handler.go b/os/gcmd/gcmd_parser_handler.go index 6093ea523..21acc11af 100644 --- a/os/gcmd/gcmd_parser_handler.go +++ b/os/gcmd/gcmd_parser_handler.go @@ -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 . +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 . func (p *Parser) RunHandle(cmd string) error { if handle, ok := p.commandFuncMap[cmd]; ok { diff --git a/text/gstr/gstr.go b/text/gstr/gstr.go index 9f64b25c5..51a2c548c 100644 --- a/text/gstr/gstr.go +++ b/text/gstr/gstr.go @@ -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 }