Skip to content

Commit

Permalink
refactor: usage using self UnquoteUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Jan 13, 2022
1 parent 81b55a1 commit 4fdcbeb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions tests/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ConfigOptionDeclareWithDefault() interface{} {
"test1": 100,
"test2": 200,
},
"Int8": int8(1),
"TimeDurations": []time.Duration([]time.Duration{time.Second, time.Second}), // @MethodComment(延迟队列)
"DefaultEmptyMap": map[string]int{},
"Int64Slice": []int64{101, 202, 303},
Expand Down
13 changes: 13 additions & 0 deletions tests/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions xflag/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,22 @@ func (f *FlagList) FlagGetAndDel(name string) *FlagInfo {
return nil
}

// UnquoteUsage linke flag.UnquoteUsage
func UnquoteUsage(ff *flag.Flag) (name string, usage string) {
varname, usage := flag.UnquoteUsage(ff)
if t, ok := ff.Value.(interface{ TypeName() string }); ok {
varname = t.TypeName()
}
if t, ok := ff.Value.(interface{ IsBoolFlag() bool }); ok && t.IsBoolFlag() {
varname = "bool"
}
return varname, usage
}

// GetFlagInfo get FlagList from given FlagSet
func GetFlagInfo(f *flag.FlagSet) (ret FlagList) {
f.VisitAll(func(ff *flag.Flag) {
varname, usage := flag.UnquoteUsage(ff)
if varname == "" || varname == "value" {
if t, ok := ff.Value.(interface{ TypeName() string }); ok {
varname = t.TypeName()
}
if t, ok := ff.Value.(interface{ IsBoolFlag() bool }); ok && t.IsBoolFlag() {
varname = "bool"
}
}
varname, usage := UnquoteUsage(ff)
v := &FlagInfo{
Name: ff.Name,
Usage: usage,
Expand Down

0 comments on commit 4fdcbeb

Please sign in to comment.