Tidy up the flag name visitor func in Context
This commit is contained in:
parent
7828300b97
commit
7318e27528
15
context.go
15
context.go
@ -137,11 +137,9 @@ func (c *Context) LocalFlagNames() []string {
|
|||||||
// its parent contexts.
|
// its parent contexts.
|
||||||
func (c *Context) FlagNames() []string {
|
func (c *Context) FlagNames() []string {
|
||||||
names := []string{}
|
names := []string{}
|
||||||
|
|
||||||
for _, ctx := range c.Lineage() {
|
for _, ctx := range c.Lineage() {
|
||||||
ctx.flagSet.Visit(makeFlagNameVisitor(&names))
|
ctx.flagSet.Visit(makeFlagNameVisitor(&names))
|
||||||
}
|
}
|
||||||
|
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,8 +360,17 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|||||||
|
|
||||||
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
|
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
|
||||||
return func(f *flag.Flag) {
|
return func(f *flag.Flag) {
|
||||||
name := strings.Split(f.Name, ",")[0]
|
nameParts := strings.Split(f.Name, ",")
|
||||||
if name != "help" && name != "version" {
|
name := strings.TrimSpace(nameParts[0])
|
||||||
|
|
||||||
|
for _, part := range nameParts {
|
||||||
|
part = strings.TrimSpace(part)
|
||||||
|
if len(part) > len(name) {
|
||||||
|
name = part
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name != "" {
|
||||||
(*names) = append(*names, name)
|
(*names) = append(*names, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user