Skip to content

Commit

Permalink
👔 up: update some for flag parse logic, update deps to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 25, 2023
1 parent 6521dc3 commit c5600f8
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 32 deletions.
10 changes: 10 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ var (
c.Ctx.Set("msg", c.App().Ctx.Get("top1:sub1"))
return nil
},
Subs: []*gcli.Command{
{
Name: "sub1-1",
Desc: "desc for top1.sub1.sub1-1",
Func: func(c *gcli.Command, args []string) error {
c.Ctx.Set("msg", c.App().Ctx.Get("top1:sub1:sub1-1"))
return nil
},
},
},
},
},
})
Expand Down
16 changes: 14 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ import (

// Runner /Executor interface
type Runner interface {
// Run Config(c *Command)
// Run the command
//
// TIP:
// args is the remain arguments after parse flags(options and arguments).
Run(c *Command, args []string) error
}

// RunnerFunc definition
//
// TIP:
//
// args is the remain arguments after parse flags(options and arguments).
type RunnerFunc func(c *Command, args []string) error

// Run implement the Runner interface
Expand Down Expand Up @@ -96,9 +103,14 @@ type Command struct {
// subName is the name for grouped commands
// eg: "sys:info" -> module: "sys", subName: "info"
// module, subName string

// Examples some usage example display
Examples string

// Func is the command handler func. Func Runner
//
// TIP:
// func `args` is the remain arguments after parse flags(options and arguments).
Func RunnerFunc
// Help is the long help message text
// Can use string-var in contents, eg: {$cmd}
Expand Down Expand Up @@ -220,7 +232,7 @@ func (c *Command) AddCommand(sub *Command) {
// do add and init sub command
c.base.addCommand(c.Name, sub)
// update some parser config
sub.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
sub.Flags.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
}

// Match sub command by input names
Expand Down
2 changes: 1 addition & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestStrings(t *testing.T) {
is.NoErr(err)
err = ss.Set("abc")
is.NoErr(err)
is.Eq("[1 3 abc]", ss.String())
is.Eq("1,3,abc", ss.String())
}

func TestBooleans(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ func NewArgument(name, desc string, requiredAndArrayed ...bool) *CliArg {
return NewArg(name, desc, nil, requiredAndArrayed...)
}

// SetArrayed the argument
// WithArrayed for the argument
func (a *CliArg) WithArrayed() *CliArg {
a.Arrayed = true
return a
}

// SetArrayed for the argument
func (a *CliArg) SetArrayed() *CliArg {
a.Arrayed = true
return a
Expand Down
5 changes: 4 additions & 1 deletion gflag/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ func TestArgument(t *testing.T) {
is.Eq("23", arg.String())

// array value
arg.Arrayed = true
arg.WithArrayed()
is.NoErr(arg.SetValue([]string{"a", "b"}))
is.True(arg.Arrayed)
is.Eq(0, arg.Int())
is.Eq("[a b]", arg.String())
is.Eq([]string{"a", "b"}, arg.Array())

arg = gcli.NewArgument("arg0", "arg desc").SetArrayed()
is.True(arg.Arrayed)

// required and is-array
arg = gflag.NewArgument("arg1", "arg desc", true, true)
arg.Init()
Expand Down
11 changes: 10 additions & 1 deletion gflag/gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ const (
// default desc
defaultDesc = "No description"
)

const (
// TagRuleNamed struct tag use named k-v rule.
//
// eg: `flag:"name=int0;shorts=i;required=true;desc=int option message"`
// eg:
// `flag:"name=int0;shorts=i;required=true;desc=int option message"`
// // name contains short name
// `flag:"name=int0,i;required=true;desc=int option message"`
TagRuleNamed uint8 = iota

// TagRuleSimple struct tag use simple rule.
// format: "desc;required;default;shorts"
//
// eg: `flag:"int option message;required;;i"`
TagRuleSimple

// TagRuleField struct tag use field name as flag setting name. TODO
//
// eg: `flag:"name,n" desc:"int option message" required:"true" default:"0"`
TagRuleField
)

// FlagTagName default tag name on struct
Expand Down
2 changes: 1 addition & 1 deletion gflag/gflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestFlags_VarOpt(t *testing.T) {
assert.NoErr(t, fs.Parse([]string{"--names", "abc", "-n", "def", "-s", "ghi"}))

assert.Len(t, ss, 3)
assert.Eq(t, "[abc def ghi]", ss.String())
assert.Eq(t, "abc,def,ghi", ss.String())
}

func TestFlags_CheckName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ func (p *Parser) FromStruct(ptr any, ruleType ...uint8) (err error) {
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
name := sf.Name

// skip cannot export field
if name[0] >= 'a' && name[0] <= 'z' {
continue
}

// TODO support anonymous field by sf.Anonymous
// eg: "name=int0;shorts=i;required=true;desc=int option message"
str := sf.Tag.Get(tagName)
if str == "" {
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.18

require (
github.com/gookit/color v1.5.3
github.com/gookit/goutil v0.6.8
golang.org/x/crypto v0.8.0
github.com/gookit/goutil v0.6.10
golang.org/x/crypto v0.7.0
)

require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE=
github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE=
github.com/gookit/goutil v0.6.8 h1:B2XXSCGav5TXWtKRT9i/s/owOLXXB7sY6UsfqeSLroE=
github.com/gookit/goutil v0.6.8/go.mod h1:u+Isykc6RQcZ4GQzulsaGm+Famd97U5Tzp3aQyo+jyA=
github.com/gookit/goutil v0.6.10 h1:iq7CXOf+fYLvrVAh3+ZoLgufGfK65TwbzE8NpnPGtyk=
github.com/gookit/goutil v0.6.10/go.mod h1:qqrPoX+Pm6YmxqqccgkNLPirTFX7UYMES1SK+fokqQU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10 changes: 4 additions & 6 deletions progress/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ var CharThemes = []rune{
CharSquare2,
}

// GetCharTheme by index number
// GetCharTheme by index number. if index not exist, will return a random theme
func GetCharTheme(index int) rune {
if len(CharThemes) > index {
if index > 0 && len(CharThemes) > index {
return CharThemes[index]
}

return RandomCharTheme()
}

// RandomCharTheme get
func RandomCharTheme() rune {
rand.Seed(time.Now().UnixNano())
return CharThemes[rand.Intn(len(CharsThemes)-1)]
return CharThemes[rand.Intn(len(CharThemes)-1)]
}

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

// GetCharsTheme by index number
func GetCharsTheme(index int) []rune {
if len(CharsThemes) > index {
if index > 0 && len(CharsThemes) > index {
return CharsThemes[index]
}

return RandomCharsTheme()
}

Expand Down

0 comments on commit c5600f8

Please sign in to comment.