improve package gcmd,internal/cmdenv

This commit is contained in:
John Guo
2020-12-04 23:29:20 +08:00
parent 57a82ebcc0
commit c226782f23
4 changed files with 43 additions and 46 deletions

View File

@ -8,33 +8,13 @@
package cmdenv
import (
"github.com/gogf/gf/os/gcmd"
"os"
"regexp"
"strings"
"github.com/gogf/gf/container/gvar"
)
var (
// Console options.
cmdOptions = make(map[string]string)
)
func init() {
doInit()
}
// doInit does the initialization for this package.
func doInit() {
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
for i := 0; i < len(os.Args); i++ {
result := reg.FindStringSubmatch(os.Args[i])
if len(result) > 1 {
cmdOptions[result[1]] = result[2]
}
}
}
// Get 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.
@ -48,7 +28,7 @@ func Get(key string, def ...interface{}) *gvar.Var {
value = def[0]
}
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
if v, ok := cmdOptions[cmdKey]; ok {
if v := gcmd.GetOpt(cmdKey); v != "" {
value = v
} else {
envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))

View File

@ -19,7 +19,6 @@ func Test_Get(t *testing.T) {
os.Args = []string{"--gf.test.value1=111"}
os.Setenv("GF_TEST_VALUE1", "222")
os.Setenv("GF_TEST_VALUE2", "333")
doInit()
gtest.C(t, func(t *gtest.T) {
t.Assert(Get("gf.test.value1").String(), "111")
t.Assert(Get("gf.test.value2").String(), "333")

View File

@ -23,25 +23,44 @@ var (
)
// Custom initialization.
func doInit() {
if len(defaultParsedArgs) > 0 {
return
func Init(args ...string) {
if len(args) == 0 {
if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 {
args = os.Args
} else {
return
}
} else {
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
}
// 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)
if len(array) == 3 {
defaultParsedOptions[array[1]] = array[2]
for i := 0; i < len(args); {
array, _ := gregex.MatchString(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`, args[i])
if len(array) > 2 {
if array[2] == "=" {
defaultParsedOptions[array[1]] = array[3]
} else if i < len(args)-1 {
if args[i+1][0] == '-' {
defaultParsedOptions[array[1]] = array[3]
} else {
defaultParsedOptions[array[1]] = args[i+1]
i += 2
continue
}
} else {
defaultParsedArgs = append(defaultParsedArgs, args[i])
}
} else {
defaultParsedArgs = append(defaultParsedArgs, arg)
defaultParsedArgs = append(defaultParsedArgs, args[i])
}
i++
}
}
// GetOpt returns the option value named <name>.
func GetOpt(name string, def ...string) string {
doInit()
Init()
if v, ok := defaultParsedOptions[name]; ok {
return v
}
@ -53,26 +72,26 @@ func GetOpt(name string, def ...string) string {
// GetOptVar returns the option value named <name> as gvar.Var.
func GetOptVar(name string, def ...string) *gvar.Var {
doInit()
Init()
return gvar.New(GetOpt(name, def...))
}
// GetOptAll returns all parsed options.
func GetOptAll() map[string]string {
doInit()
Init()
return defaultParsedOptions
}
// ContainsOpt checks whether option named <name> exist in the arguments.
func ContainsOpt(name string, def ...string) bool {
doInit()
Init()
_, ok := defaultParsedOptions[name]
return ok
}
// GetArg returns the argument at <index>.
func GetArg(index int, def ...string) string {
doInit()
Init()
if index < len(defaultParsedArgs) {
return defaultParsedArgs[index]
}
@ -84,13 +103,13 @@ func GetArg(index int, def ...string) string {
// GetArgVar returns the argument at <index> as gvar.Var.
func GetArgVar(index int, def ...string) *gvar.Var {
doInit()
Init()
return gvar.New(GetArg(index, def...))
}
// GetArgAll returns all parsed arguments.
func GetArgAll() []string {
doInit()
Init()
return defaultParsedArgs
}

View File

@ -9,7 +9,6 @@
package gcmd_test
import (
"os"
"testing"
"github.com/gogf/gf/frame/g"
@ -20,16 +19,16 @@ import (
func Test_Default(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
os.Args = []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}
t.Assert(len(gcmd.GetArgAll()), 4)
t.Assert(gcmd.GetArg(1), "remove")
gcmd.Init([]string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}...)
t.Assert(len(gcmd.GetArgAll()), 2)
t.Assert(gcmd.GetArg(1), "path")
t.Assert(gcmd.GetArg(100, "test"), "test")
t.Assert(gcmd.GetOpt("n"), "")
t.Assert(gcmd.GetOpt("force"), "remove")
t.Assert(gcmd.GetOpt("n"), "root")
t.Assert(gcmd.ContainsOpt("fq"), true)
t.Assert(gcmd.ContainsOpt("p"), true)
t.Assert(gcmd.ContainsOpt("n"), true)
t.Assert(gcmd.ContainsOpt("none"), false)
t.Assert(gcmd.GetOpt("none", "value"), "value")
})
}