Skip to content

Commit

Permalink
✨ feat(gflag): add new flag parse config IndentLongOpt
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 25, 2023
1 parent 69d1c14 commit 21da8f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
12 changes: 12 additions & 0 deletions gflag/gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
// FlagTagName default tag name on struct
var FlagTagName = "flag"

// ConfigFunc config func for parser
type ConfigFunc func(cfg *Config)

// Config for render help information
type Config struct {
// WithoutType don't display flag data type on print help
Expand All @@ -44,6 +47,8 @@ type Config struct {
TagRuleType uint8
// DisableArg disable binding arguments.
DisableArg bool
// IndentLongOpt indent long option name on print help
IndentLongOpt bool
}

// GetTagName get tag name, default is FlagTagName
Expand All @@ -54,6 +59,13 @@ func (c *Config) GetTagName() string {
return c.TagName
}

// WithIndentLongOpt on print help
func WithIndentLongOpt(yes bool) ConfigFunc {
return func(cfg *Config) {
cfg.IndentLongOpt = yes
}
}

// OptCategory struct
type OptCategory struct {
Name, Title string
Expand Down
25 changes: 24 additions & 1 deletion gflag/gflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,34 @@ func TestFlags_PrintHelpPanel(t *testing.T) {

fs.IntVar(&testOpts.opt1, &gcli.FlagMeta{Name: "opt1"})
fs.StrVar(&testOpts.opt3, &gcli.FlagMeta{
Name: "test",
Name: "test, t",
Desc: "test desc",
// required
Required: true,
})
fs.BoolOpt(&testOpts.opt2, "bol", "ab", false, "opt2 desc")
fs.PrintHelpPanel()
}

func TestFlags_PrintHelpPanel_IndentLongOpt(t *testing.T) {
fs := gflag.New("test").WithConfigFn(func(c *gflag.Config) {
c.IndentLongOpt = true
})

testOpts := struct {
opt1 int
opt2 bool
opt3 string
}{}

fs.IntVar(&testOpts.opt1, &gcli.FlagMeta{Name: "opt1"})
fs.StrVar(&testOpts.opt3, &gcli.FlagMeta{
Name: "test, t",
Desc: "test desc",
// required
Required: true,
})
fs.BoolOpt(&testOpts.opt2, "bol", "ab", false, "opt2 desc")
fmt.Println("Flag Help - enable IndentLongOpt:")
fs.PrintHelpPanel()
}
5 changes: 5 additions & 0 deletions gflag/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (p *Parser) formatOneFlag(f *flag.Flag) (s string) {

// add prefix '-' to option
fullName = cflag.AddPrefixes2(name, opt.Shorts, true)
if p.hasShort && p.cfg.IndentLongOpt && fullName[1] == '-' {
nameLen += 4
fullName = " " + fullName
}

s = fmt.Sprintf(" <info>%s</>", fullName)

// - build flag type info
Expand Down
7 changes: 6 additions & 1 deletion gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (p *Parser) SetName(name string) {
p.CliArgs.SetName(name)
}

// ParserCfg for the parser.
func (p *Parser) ParserCfg() *Config {
return p.cfg
}

// SetConfig for the object.
func (p *Parser) SetConfig(opt *Config) { p.cfg = opt }

Expand All @@ -113,7 +118,7 @@ func (p *Parser) SetRuleType(rt uint8) *Parser {
}

// WithConfigFn for the object.
func (p *Parser) WithConfigFn(fns ...func(cfg *Config)) *Parser {
func (p *Parser) WithConfigFn(fns ...ConfigFunc) *Parser {
for _, fn := range fns {
fn(p.cfg)
}
Expand Down

0 comments on commit 21da8f9

Please sign in to comment.