Skip to content

Commit 78e8fdf

Browse files
committed
refactor: move the HelpVars to sub pkg helper
1 parent 55c2a8b commit 78e8fdf

File tree

4 files changed

+104
-90
lines changed

4 files changed

+104
-90
lines changed

base.go

+3-64
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gcli
22

33
import (
4-
"fmt"
54
"os"
65
"path"
76
"path/filepath"
@@ -10,6 +9,7 @@ import (
109
"strings"
1110

1211
"github.com/gookit/color"
12+
"github.com/gookit/gcli/v3/helper"
1313
"github.com/gookit/goutil/cflag"
1414
"github.com/gookit/goutil/mathutil"
1515
"github.com/gookit/goutil/structs"
@@ -23,7 +23,7 @@ type core struct {
2323
// Hooks manage. allowed hooks: "init", "before", "after", "error"
2424
*Hooks
2525
// HelpVars help template vars.
26-
HelpVars
26+
helper.HelpVars
2727
// global options flag set
2828
gFlags *Flags
2929
// GOptsBinder you can be custom binding global options
@@ -82,7 +82,7 @@ func (c core) innerHelpVars() map[string]string {
8282
}
8383

8484
// simple map[string]any struct
85-
// TODO use structs.DataStore
85+
// TODO use structs.Data
8686
type mapData struct {
8787
data map[string]any
8888
}
@@ -222,70 +222,9 @@ func (c *cmdLine) hasHelpKeywords() bool {
222222
if c.argLine == "" {
223223
return false
224224
}
225-
226225
return strings.HasSuffix(c.argLine, " -h") || strings.HasSuffix(c.argLine, " --help")
227226
}
228227

229-
/*************************************************************
230-
* app/cmd help vars
231-
*************************************************************/
232-
233-
// HelpVarFormat allow var replace on render help info.
234-
//
235-
// Default support:
236-
//
237-
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
238-
const HelpVarFormat = "{$%s}"
239-
240-
// HelpVars struct. provide string var function for render help template.
241-
type HelpVars struct {
242-
// varLeft, varRight string
243-
// varFormat string
244-
// Vars you can add some vars map for render help info
245-
Vars map[string]string
246-
}
247-
248-
// AddVar get command name
249-
func (hv *HelpVars) AddVar(name, value string) {
250-
if hv.Vars == nil {
251-
hv.Vars = make(map[string]string)
252-
}
253-
254-
hv.Vars[name] = value
255-
}
256-
257-
// AddVars add multi tpl vars
258-
func (hv *HelpVars) AddVars(vars map[string]string) {
259-
for n, v := range vars {
260-
hv.AddVar(n, v)
261-
}
262-
}
263-
264-
// GetVar get a help var by name
265-
func (hv *HelpVars) GetVar(name string) string {
266-
return hv.Vars[name]
267-
}
268-
269-
// GetVars get all tpl vars
270-
func (hv *HelpVars) GetVars() map[string]string {
271-
return hv.Vars
272-
}
273-
274-
// ReplaceVars replace vars in the input string.
275-
func (hv *HelpVars) ReplaceVars(input string) string {
276-
// if not use var
277-
if !strings.Contains(input, "{$") {
278-
return input
279-
}
280-
281-
var ss []string
282-
for n, v := range hv.Vars {
283-
ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v)
284-
}
285-
286-
return strings.NewReplacer(ss...).Replace(input)
287-
}
288-
289228
/*************************************************************
290229
* command Base
291230
*************************************************************/

helper/help_vars.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
/*************************************************************
9+
* app/cmd help vars
10+
*************************************************************/
11+
12+
// HelpVarFormat allow var replace on render help info.
13+
//
14+
// Default support:
15+
//
16+
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
17+
const HelpVarFormat = "{$%s}"
18+
19+
// HelpVars struct. provide string var function for render help template.
20+
type HelpVars struct {
21+
VarOpen, VarClose string
22+
23+
// Vars you can add some vars map for render help info
24+
Vars map[string]string
25+
}
26+
27+
// AddVar get command name
28+
func (hv *HelpVars) AddVar(name, value string) {
29+
if hv.Vars == nil {
30+
hv.Vars = make(map[string]string)
31+
}
32+
33+
hv.Vars[name] = value
34+
}
35+
36+
// AddVars add multi tpl vars
37+
func (hv *HelpVars) AddVars(vars map[string]string) {
38+
for n, v := range vars {
39+
hv.AddVar(n, v)
40+
}
41+
}
42+
43+
// GetVar get a help var by name
44+
func (hv *HelpVars) GetVar(name string) string {
45+
return hv.Vars[name]
46+
}
47+
48+
// GetVars get all tpl vars
49+
func (hv *HelpVars) GetVars() map[string]string {
50+
return hv.Vars
51+
}
52+
53+
// ReplaceVars replace vars in the input string.
54+
func (hv *HelpVars) ReplaceVars(input string) string {
55+
// if not use var
56+
if !strings.Contains(input, "{$") {
57+
return input
58+
}
59+
60+
var ss []string
61+
for n, v := range hv.Vars {
62+
ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v)
63+
}
64+
65+
return strings.NewReplacer(ss...).Replace(input)
66+
}

helper/help_vars_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package helper_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gookit/gcli/v3/helper"
7+
"github.com/gookit/goutil/testutil/assert"
8+
)
9+
10+
func TestHelpVars(t *testing.T) {
11+
is := assert.New(t)
12+
vs := helper.HelpVars{
13+
Vars: map[string]string{
14+
"key0": "val0",
15+
"key1": "val1",
16+
},
17+
}
18+
19+
is.Len(vs.GetVars(), 2)
20+
is.Contains(vs.GetVars(), "key0")
21+
22+
vs.AddVars(map[string]string{"key2": "val2"})
23+
vs.AddVar("key3", "val3")
24+
25+
is.Eq("val3", vs.GetVar("key3"))
26+
is.Eq("", vs.GetVar("not-exist"))
27+
28+
is.Eq("hello val0", vs.ReplaceVars("hello {$key0}"))
29+
is.Eq("hello val0 val2", vs.ReplaceVars("hello {$key0} {$key2}"))
30+
// invalid input
31+
is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}"))
32+
}

util_test.go

+3-26
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,21 @@ import (
77
"github.com/gookit/goutil/testutil/assert"
88
)
99

10-
func TestHelpVars(t *testing.T) {
11-
is := assert.New(t)
12-
vs := gcli.HelpVars{
13-
Vars: map[string]string{
14-
"key0": "val0",
15-
"key1": "val1",
16-
},
17-
}
18-
19-
is.Len(vs.GetVars(), 2)
20-
is.Contains(vs.GetVars(), "key0")
21-
22-
vs.AddVars(map[string]string{"key2": "val2"})
23-
vs.AddVar("key3", "val3")
24-
25-
is.Eq("val3", vs.GetVar("key3"))
26-
is.Eq("", vs.GetVar("not-exist"))
27-
28-
is.Eq("hello val0", vs.ReplaceVars("hello {$key0}"))
29-
is.Eq("hello val0 val2", vs.ReplaceVars("hello {$key0} {$key2}"))
30-
// invalid input
31-
is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}"))
32-
}
33-
3410
func Test_strictFormatArgs(t *testing.T) {
3511
str1 := ""
3612
t1 := false
3713
t2 := false
3814
t3 := false
39-
//t4 := false
15+
// t4 := false
4016
is := assert.New(t)
4117
cmd := gcli.NewCommand("init", "test bool pare", func(c *gcli.Command) {
4218
c.StrOpt(&str1, "name", "n", "", "test string parse")
4319
c.BoolOpt(&t1, "test1", "t", false, "test bool arse")
4420
c.BoolOpt(&t2, "test2", "s", false, "test bool arse")
4521
c.BoolOpt(&t3, "test3", "c", true, "test bool arse")
46-
//c.BoolOpt(&t4, "test4", "d", false, "test bool arse")
22+
// c.BoolOpt(&t4, "test4", "d", false, "test bool arse")
4723
})
24+
4825
err := cmd.Run([]string{"-n", "ccc", "-test1=true", "-s", "--test3=false"})
4926
is.NoErr(err)
5027
is.Eq("ccc", str1)

0 commit comments

Comments
 (0)