diff --git a/base.go b/base.go index a45bea5..28a7395 100644 --- a/base.go +++ b/base.go @@ -1,7 +1,6 @@ package gcli import ( - "fmt" "os" "path" "path/filepath" @@ -10,6 +9,7 @@ import ( "strings" "github.com/gookit/color" + "github.com/gookit/gcli/v3/helper" "github.com/gookit/goutil/cflag" "github.com/gookit/goutil/mathutil" "github.com/gookit/goutil/structs" @@ -23,7 +23,7 @@ type core struct { // Hooks manage. allowed hooks: "init", "before", "after", "error" *Hooks // HelpVars help template vars. - HelpVars + helper.HelpVars // global options flag set gFlags *Flags // GOptsBinder you can be custom binding global options @@ -82,7 +82,7 @@ func (c core) innerHelpVars() map[string]string { } // simple map[string]any struct -// TODO use structs.DataStore +// TODO use structs.Data type mapData struct { data map[string]any } @@ -222,70 +222,9 @@ func (c *cmdLine) hasHelpKeywords() bool { if c.argLine == "" { return false } - return strings.HasSuffix(c.argLine, " -h") || strings.HasSuffix(c.argLine, " --help") } -/************************************************************* - * app/cmd help vars - *************************************************************/ - -// HelpVarFormat allow var replace on render help info. -// -// Default support: -// -// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}" -const HelpVarFormat = "{$%s}" - -// HelpVars struct. provide string var function for render help template. -type HelpVars struct { - // varLeft, varRight string - // varFormat string - // Vars you can add some vars map for render help info - Vars map[string]string -} - -// AddVar get command name -func (hv *HelpVars) AddVar(name, value string) { - if hv.Vars == nil { - hv.Vars = make(map[string]string) - } - - hv.Vars[name] = value -} - -// AddVars add multi tpl vars -func (hv *HelpVars) AddVars(vars map[string]string) { - for n, v := range vars { - hv.AddVar(n, v) - } -} - -// GetVar get a help var by name -func (hv *HelpVars) GetVar(name string) string { - return hv.Vars[name] -} - -// GetVars get all tpl vars -func (hv *HelpVars) GetVars() map[string]string { - return hv.Vars -} - -// ReplaceVars replace vars in the input string. -func (hv *HelpVars) ReplaceVars(input string) string { - // if not use var - if !strings.Contains(input, "{$") { - return input - } - - var ss []string - for n, v := range hv.Vars { - ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v) - } - - return strings.NewReplacer(ss...).Replace(input) -} - /************************************************************* * command Base *************************************************************/ diff --git a/helper/help_vars.go b/helper/help_vars.go new file mode 100644 index 0000000..7fb9299 --- /dev/null +++ b/helper/help_vars.go @@ -0,0 +1,66 @@ +package helper + +import ( + "fmt" + "strings" +) + +/************************************************************* + * app/cmd help vars + *************************************************************/ + +// HelpVarFormat allow var replace on render help info. +// +// Default support: +// +// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}" +const HelpVarFormat = "{$%s}" + +// HelpVars struct. provide string var function for render help template. +type HelpVars struct { + VarOpen, VarClose string + + // Vars you can add some vars map for render help info + Vars map[string]string +} + +// AddVar get command name +func (hv *HelpVars) AddVar(name, value string) { + if hv.Vars == nil { + hv.Vars = make(map[string]string) + } + + hv.Vars[name] = value +} + +// AddVars add multi tpl vars +func (hv *HelpVars) AddVars(vars map[string]string) { + for n, v := range vars { + hv.AddVar(n, v) + } +} + +// GetVar get a help var by name +func (hv *HelpVars) GetVar(name string) string { + return hv.Vars[name] +} + +// GetVars get all tpl vars +func (hv *HelpVars) GetVars() map[string]string { + return hv.Vars +} + +// ReplaceVars replace vars in the input string. +func (hv *HelpVars) ReplaceVars(input string) string { + // if not use var + if !strings.Contains(input, "{$") { + return input + } + + var ss []string + for n, v := range hv.Vars { + ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v) + } + + return strings.NewReplacer(ss...).Replace(input) +} diff --git a/helper/help_vars_test.go b/helper/help_vars_test.go new file mode 100644 index 0000000..0476e42 --- /dev/null +++ b/helper/help_vars_test.go @@ -0,0 +1,32 @@ +package helper_test + +import ( + "testing" + + "github.com/gookit/gcli/v3/helper" + "github.com/gookit/goutil/testutil/assert" +) + +func TestHelpVars(t *testing.T) { + is := assert.New(t) + vs := helper.HelpVars{ + Vars: map[string]string{ + "key0": "val0", + "key1": "val1", + }, + } + + is.Len(vs.GetVars(), 2) + is.Contains(vs.GetVars(), "key0") + + vs.AddVars(map[string]string{"key2": "val2"}) + vs.AddVar("key3", "val3") + + is.Eq("val3", vs.GetVar("key3")) + is.Eq("", vs.GetVar("not-exist")) + + is.Eq("hello val0", vs.ReplaceVars("hello {$key0}")) + is.Eq("hello val0 val2", vs.ReplaceVars("hello {$key0} {$key2}")) + // invalid input + is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}")) +} diff --git a/util_test.go b/util_test.go index 6545c07..dd16a01 100644 --- a/util_test.go +++ b/util_test.go @@ -7,44 +7,21 @@ import ( "github.com/gookit/goutil/testutil/assert" ) -func TestHelpVars(t *testing.T) { - is := assert.New(t) - vs := gcli.HelpVars{ - Vars: map[string]string{ - "key0": "val0", - "key1": "val1", - }, - } - - is.Len(vs.GetVars(), 2) - is.Contains(vs.GetVars(), "key0") - - vs.AddVars(map[string]string{"key2": "val2"}) - vs.AddVar("key3", "val3") - - is.Eq("val3", vs.GetVar("key3")) - is.Eq("", vs.GetVar("not-exist")) - - is.Eq("hello val0", vs.ReplaceVars("hello {$key0}")) - is.Eq("hello val0 val2", vs.ReplaceVars("hello {$key0} {$key2}")) - // invalid input - is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}")) -} - func Test_strictFormatArgs(t *testing.T) { str1 := "" t1 := false t2 := false t3 := false - //t4 := false + // t4 := false is := assert.New(t) cmd := gcli.NewCommand("init", "test bool pare", func(c *gcli.Command) { c.StrOpt(&str1, "name", "n", "", "test string parse") c.BoolOpt(&t1, "test1", "t", false, "test bool arse") c.BoolOpt(&t2, "test2", "s", false, "test bool arse") c.BoolOpt(&t3, "test3", "c", true, "test bool arse") - //c.BoolOpt(&t4, "test4", "d", false, "test bool arse") + // c.BoolOpt(&t4, "test4", "d", false, "test bool arse") }) + err := cmd.Run([]string{"-n", "ccc", "-test1=true", "-s", "--test3=false"}) is.NoErr(err) is.Eq("ccc", str1)