diff --git a/g/internal/cmdenv/cmdenv.go b/g/internal/cmdenv/cmdenv.go index 9b843ce00..a81afafec 100644 --- a/g/internal/cmdenv/cmdenv.go +++ b/g/internal/cmdenv/cmdenv.go @@ -19,7 +19,12 @@ var ( cmdOptions = make(map[string]string) ) -func init() { +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]) diff --git a/g/internal/cmdenv/cmdenv_test.go b/g/internal/cmdenv/cmdenv_test.go new file mode 100644 index 000000000..cde037eed --- /dev/null +++ b/g/internal/cmdenv/cmdenv_test.go @@ -0,0 +1,29 @@ +// 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. + +// go test *.go -bench=".*" -benchmem + +package cmdenv + +import ( + "github.com/gogf/gf/g/test/gtest" + "os" + "testing" +) + +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.Case(t, func() { + gtest.Assert(Get("gf.test.value1").String(), "111") + gtest.Assert(Get("gf.test.value2").String(), "333") + gtest.Assert(Get("gf.test.value3").String(), "") + gtest.Assert(Get("gf.test.value3", 1).String(), "1") + }) +} + diff --git a/g/os/gcmd/gcmd.go b/g/os/gcmd/gcmd.go index db7f8e2c6..35f2b0bb9 100644 --- a/g/os/gcmd/gcmd.go +++ b/g/os/gcmd/gcmd.go @@ -24,13 +24,19 @@ type gCmdOption struct { options map[string]string } -var Value = &gCmdValue{} // Console values. -var Option = &gCmdOption{} // Console options. +var Value = &gCmdValue{} // Console values. +var Option = &gCmdOption{} // Console options. var cmdFuncMap = make(map[string]func()) // Registered callback functions. func init() { - reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`) + doInit() +} + +// doInit does the initialization for this package. +func doInit() { + Value.values = Value.values[:0] Option.options = make(map[string]string) + reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`) for i := 0; i < len(os.Args); i++ { result := reg.FindStringSubmatch(os.Args[i]) if len(result) > 1 { diff --git a/g/os/gcmd/gcmd_z_unit_test.go b/g/os/gcmd/gcmd_z_unit_test.go new file mode 100644 index 000000000..bd9e05a84 --- /dev/null +++ b/g/os/gcmd/gcmd_z_unit_test.go @@ -0,0 +1,28 @@ +// 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. + +// go test *.go -bench=".*" -benchmem + +package gcmd + +import ( + "github.com/gogf/gf/g/test/gtest" + "os" + "testing" +) + + +func Test_ValueAndOption(t *testing.T) { + os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"} + doInit() + gtest.Case(t, func() { + gtest.Assert(Value.GetAll(), []string{"v1", "v2"}) + gtest.Assert(Value.Get(0), "v1") + gtest.Assert(Value.Get(1), "v2") + gtest.Assert(Value.Get(2), "") + }) +} +