Skip to content

Commit

Permalink
👔 up(gflag): update the binding options from struct logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2023
1 parent 9ee8ea0 commit f00baba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
18 changes: 13 additions & 5 deletions gflag/gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,14 +38,22 @@ 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
// DisableArg disable binding arguments.
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
Expand Down
24 changes: 19 additions & 5 deletions gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f00baba

Please sign in to comment.