From f00baba9422584174dcfc0667e159f4dcfa45c78 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 24 Mar 2023 13:16:34 +0800 Subject: [PATCH] :necktie: up(gflag): update the binding options from struct logic --- gflag/gflag.go | 18 +++++++++++++----- gflag/parser.go | 24 +++++++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/gflag/gflag.go b/gflag/gflag.go index c4d0bef..a0779ba 100644 --- a/gflag/gflag.go +++ b/gflag/gflag.go @@ -11,20 +11,20 @@ const ( AlignLeft = strutil.PosRight // AlignRight Align left, padding right AlignRight = strutil.PosLeft - // 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"` - TagRuleNamed = 0 + TagRuleNamed uint8 = iota // TagRuleSimple struct tag use simple rule. // format: "desc;required;default;shorts" // // eg: `flag:"int option message;required;;i"` - TagRuleSimple = 1 + TagRuleSimple ) // FlagTagName default tag name on struct @@ -38,7 +38,7 @@ type Config struct { DescNewline bool // Alignment flag name align left or right. default is: left Alignment strutil.PosFlag - // TagName on struct + // TagName on struct. default is FlagTagName TagName string // TagRuleType for struct tag value. default is TagRuleNamed TagRuleType uint8 @@ -46,6 +46,14 @@ type Config struct { DisableArg bool } +// GetTagName get tag name, default is FlagTagName +func (c *Config) GetTagName() string { + if c.TagName == "" { + c.TagName = FlagTagName + } + return c.TagName +} + // OptCategory struct type OptCategory struct { Name, Title string diff --git a/gflag/parser.go b/gflag/parser.go index e003051..c734a50 100644 --- a/gflag/parser.go +++ b/gflag/parser.go @@ -12,6 +12,7 @@ import ( "github.com/gookit/color" "github.com/gookit/gcli/v3/helper" + "github.com/gookit/goutil" "github.com/gookit/goutil/cflag" "github.com/gookit/goutil/structs" "github.com/gookit/goutil/strutil" @@ -105,6 +106,12 @@ func (p *Parser) UseSimpleRule() *Parser { return p } +// SetRuleType for the parse tag value rule string. +func (p *Parser) SetRuleType(rt uint8) *Parser { + p.cfg.TagRuleType = rt + return p +} + // WithConfigFn for the object. func (p *Parser) WithConfigFn(fns ...func(cfg *Config)) *Parser { for _, fn := range fns { @@ -233,6 +240,13 @@ var ( errTagRuleType = errors.New("invalid tag rule type on struct") ) +// MustFromStruct from struct tag binding options, panic if error +// +// more see FromStruct() +func (p *Parser) MustFromStruct(ptr any, ruleType ...uint8) { + goutil.MustOK(p.FromStruct(ptr, ruleType...)) +} + // FromStruct from struct tag binding options // // ## Named rule: @@ -254,8 +268,8 @@ var ( // Age int `flag:"age;input user age;true;;o"` // } // opt := &UserCmdOpts{} -// p.UseSimpleRule().FromStruct(opt) -func (p *Parser) FromStruct(ptr any) (err error) { +// p.FromStruct(opt, gflag.TagRuleSimple) +func (p *Parser) FromStruct(ptr any, ruleType ...uint8) (err error) { v := reflect.ValueOf(ptr) if v.Kind() != reflect.Ptr { return errNotPtrValue @@ -270,9 +284,9 @@ func (p *Parser) FromStruct(ptr any) (err error) { return errNotAnStruct } - tagName := p.cfg.TagName - if tagName == "" { - tagName = FlagTagName + tagName := p.cfg.GetTagName() + if len(ruleType) > 0 { + p.SetRuleType(ruleType[0]) } var mp map[string]string