Skip to content

Commit c5600f8

Browse files
committed
👔 up: update some for flag parse logic, update deps to latest
1 parent 6521dc3 commit c5600f8

11 files changed

+70
-32
lines changed

app_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ var (
4848
c.Ctx.Set("msg", c.App().Ctx.Get("top1:sub1"))
4949
return nil
5050
},
51+
Subs: []*gcli.Command{
52+
{
53+
Name: "sub1-1",
54+
Desc: "desc for top1.sub1.sub1-1",
55+
Func: func(c *gcli.Command, args []string) error {
56+
c.Ctx.Set("msg", c.App().Ctx.Get("top1:sub1:sub1-1"))
57+
return nil
58+
},
59+
},
60+
},
5161
},
5262
},
5363
})

cmd.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ import (
1818

1919
// Runner /Executor interface
2020
type Runner interface {
21-
// Run Config(c *Command)
21+
// Run the command
22+
//
23+
// TIP:
24+
// args is the remain arguments after parse flags(options and arguments).
2225
Run(c *Command, args []string) error
2326
}
2427

2528
// RunnerFunc definition
29+
//
30+
// TIP:
31+
//
32+
// args is the remain arguments after parse flags(options and arguments).
2633
type RunnerFunc func(c *Command, args []string) error
2734

2835
// Run implement the Runner interface
@@ -96,9 +103,14 @@ type Command struct {
96103
// subName is the name for grouped commands
97104
// eg: "sys:info" -> module: "sys", subName: "info"
98105
// module, subName string
106+
99107
// Examples some usage example display
100108
Examples string
109+
101110
// Func is the command handler func. Func Runner
111+
//
112+
// TIP:
113+
// func `args` is the remain arguments after parse flags(options and arguments).
102114
Func RunnerFunc
103115
// Help is the long help message text
104116
// Can use string-var in contents, eg: {$cmd}
@@ -220,7 +232,7 @@ func (c *Command) AddCommand(sub *Command) {
220232
// do add and init sub command
221233
c.base.addCommand(c.Name, sub)
222234
// update some parser config
223-
sub.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
235+
sub.Flags.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
224236
}
225237

226238
// Match sub command by input names

cmd_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func TestStrings(t *testing.T) {
416416
is.NoErr(err)
417417
err = ss.Set("abc")
418418
is.NoErr(err)
419-
is.Eq("[1 3 abc]", ss.String())
419+
is.Eq("1,3,abc", ss.String())
420420
}
421421

422422
func TestBooleans(t *testing.T) {

gflag/args.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,13 @@ func NewArgument(name, desc string, requiredAndArrayed ...bool) *CliArg {
330330
return NewArg(name, desc, nil, requiredAndArrayed...)
331331
}
332332

333-
// SetArrayed the argument
333+
// WithArrayed for the argument
334+
func (a *CliArg) WithArrayed() *CliArg {
335+
a.Arrayed = true
336+
return a
337+
}
338+
339+
// SetArrayed for the argument
334340
func (a *CliArg) SetArrayed() *CliArg {
335341
a.Arrayed = true
336342
return a

gflag/args_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,16 @@ func TestArgument(t *testing.T) {
118118
is.Eq("23", arg.String())
119119

120120
// array value
121-
arg.Arrayed = true
121+
arg.WithArrayed()
122122
is.NoErr(arg.SetValue([]string{"a", "b"}))
123123
is.True(arg.Arrayed)
124124
is.Eq(0, arg.Int())
125125
is.Eq("[a b]", arg.String())
126126
is.Eq([]string{"a", "b"}, arg.Array())
127127

128+
arg = gcli.NewArgument("arg0", "arg desc").SetArrayed()
129+
is.True(arg.Arrayed)
130+
128131
// required and is-array
129132
arg = gflag.NewArgument("arg1", "arg desc", true, true)
130133
arg.Init()

gflag/gflag.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@ const (
1414
// default desc
1515
defaultDesc = "No description"
1616
)
17+
1718
const (
1819
// TagRuleNamed struct tag use named k-v rule.
1920
//
20-
// eg: `flag:"name=int0;shorts=i;required=true;desc=int option message"`
21+
// eg:
22+
// `flag:"name=int0;shorts=i;required=true;desc=int option message"`
23+
// // name contains short name
24+
// `flag:"name=int0,i;required=true;desc=int option message"`
2125
TagRuleNamed uint8 = iota
2226

2327
// TagRuleSimple struct tag use simple rule.
2428
// format: "desc;required;default;shorts"
2529
//
2630
// eg: `flag:"int option message;required;;i"`
2731
TagRuleSimple
32+
33+
// TagRuleField struct tag use field name as flag setting name. TODO
34+
//
35+
// eg: `flag:"name,n" desc:"int option message" required:"true" default:"0"`
36+
TagRuleField
2837
)
2938

3039
// FlagTagName default tag name on struct

gflag/gflag_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestFlags_VarOpt(t *testing.T) {
208208
assert.NoErr(t, fs.Parse([]string{"--names", "abc", "-n", "def", "-s", "ghi"}))
209209

210210
assert.Len(t, ss, 3)
211-
assert.Eq(t, "[abc def ghi]", ss.String())
211+
assert.Eq(t, "abc,def,ghi", ss.String())
212212
}
213213

214214
func TestFlags_CheckName(t *testing.T) {

gflag/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ func (p *Parser) FromStruct(ptr any, ruleType ...uint8) (err error) {
298298
for i := 0; i < t.NumField(); i++ {
299299
sf := t.Field(i)
300300
name := sf.Name
301-
302301
// skip cannot export field
303302
if name[0] >= 'a' && name[0] <= 'z' {
304303
continue
305304
}
306305

306+
// TODO support anonymous field by sf.Anonymous
307307
// eg: "name=int0;shorts=i;required=true;desc=int option message"
308308
str := sf.Tag.Get(tagName)
309309
if str == "" {

go.mod

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ go 1.18
44

55
require (
66
github.com/gookit/color v1.5.3
7-
github.com/gookit/goutil v0.6.8
8-
golang.org/x/crypto v0.8.0
7+
github.com/gookit/goutil v0.6.10
8+
golang.org/x/crypto v0.7.0
99
)
1010

1111
require (
1212
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
13-
golang.org/x/sync v0.1.0 // indirect
14-
golang.org/x/sys v0.7.0 // indirect
15-
golang.org/x/term v0.7.0 // indirect
16-
golang.org/x/text v0.9.0 // indirect
13+
golang.org/x/sync v0.3.0 // indirect
14+
golang.org/x/sys v0.9.0 // indirect
15+
golang.org/x/term v0.9.0 // indirect
16+
golang.org/x/text v0.10.0 // indirect
1717
)

go.sum

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE=
33
github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE=
4-
github.com/gookit/goutil v0.6.8 h1:B2XXSCGav5TXWtKRT9i/s/owOLXXB7sY6UsfqeSLroE=
5-
github.com/gookit/goutil v0.6.8/go.mod h1:u+Isykc6RQcZ4GQzulsaGm+Famd97U5Tzp3aQyo+jyA=
4+
github.com/gookit/goutil v0.6.10 h1:iq7CXOf+fYLvrVAh3+ZoLgufGfK65TwbzE8NpnPGtyk=
5+
github.com/gookit/goutil v0.6.10/go.mod h1:qqrPoX+Pm6YmxqqccgkNLPirTFX7UYMES1SK+fokqQU=
66
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
77
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
88
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
99
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
10-
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
11-
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
10+
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
11+
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
1212
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
13-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
14-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15-
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
16-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17-
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
18-
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
19-
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
20-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
13+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
14+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
15+
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
16+
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
18+
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
19+
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
20+
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
2121
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

progress/helper.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ var CharThemes = []rune{
2222
CharSquare2,
2323
}
2424

25-
// GetCharTheme by index number
25+
// GetCharTheme by index number. if index not exist, will return a random theme
2626
func GetCharTheme(index int) rune {
27-
if len(CharThemes) > index {
27+
if index > 0 && len(CharThemes) > index {
2828
return CharThemes[index]
2929
}
30-
3130
return RandomCharTheme()
3231
}
3332

3433
// RandomCharTheme get
3534
func RandomCharTheme() rune {
3635
rand.Seed(time.Now().UnixNano())
37-
return CharThemes[rand.Intn(len(CharsThemes)-1)]
36+
return CharThemes[rand.Intn(len(CharThemes)-1)]
3837
}
3938

4039
// CharsThemes collection. can use for LoadingBar, LoadingSpinner
@@ -69,10 +68,9 @@ var CharsThemes = [][]rune{
6968

7069
// GetCharsTheme by index number
7170
func GetCharsTheme(index int) []rune {
72-
if len(CharsThemes) > index {
71+
if index > 0 && len(CharsThemes) > index {
7372
return CharsThemes[index]
7473
}
75-
7674
return RandomCharsTheme()
7775
}
7876

0 commit comments

Comments
 (0)