Skip to content

Commit

Permalink
refactor: move the HelpVars to sub pkg helper
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 4, 2022
1 parent 55c2a8b commit 78e8fdf
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 90 deletions.
67 changes: 3 additions & 64 deletions base.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gcli

import (
"fmt"
"os"
"path"
"path/filepath"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
*************************************************************/
Expand Down
66 changes: 66 additions & 0 deletions helper/help_vars.go
Original file line number Diff line number Diff line change
@@ -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)
}
32 changes: 32 additions & 0 deletions helper/help_vars_test.go
Original file line number Diff line number Diff line change
@@ -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}"))
}
29 changes: 3 additions & 26 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 78e8fdf

Please sign in to comment.