diff options
| author | Hugo Hromic <[email protected]> | 2023-07-15 13:25:46 +0100 |
|---|---|---|
| committer | Hugo Hromic <[email protected]> | 2024-06-29 23:42:22 +0100 |
| commit | 4ed4ce751fa49ae1b04f672c1092120fd71d5b21 (patch) | |
| tree | d3433c75a24948d2d393c918d612badf750cc430 | |
| parent | bee5cf5d7cc07c41b2a528052bfba0566b5069c0 (diff) | |
Better scanning of version flag in specs for help generation
| -rw-r--r-- | usage.go | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -208,6 +208,9 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error positionals = append(positionals, spec) case spec.long != "": longOptions = append(longOptions, spec) + if spec.long == "version" { + hasVersionOption = true + } case spec.short != "": shortOptions = append(shortOptions, spec) case spec.short == "" && spec.long == "": @@ -215,6 +218,21 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error } } + // obtain a flattened list of options from all ancestors + // also determine if any ancestor has a version option spec + var globals []*spec + ancestor := cmd.parent + for ancestor != nil { + for _, spec := range ancestor.specs { + if spec.long == "version" { + hasVersionOption = true + break + } + } + globals = append(globals, ancestor.specs...) + ancestor = ancestor.parent + } + if p.description != "" { fmt.Fprintln(w, p.description) } @@ -236,28 +254,14 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error } for _, spec := range longOptions { p.printOption(w, spec) - if spec.long == "version" { - hasVersionOption = true - } } } - // obtain a flattened list of options from all ancestors - var globals []*spec - ancestor := cmd.parent - for ancestor != nil { - globals = append(globals, ancestor.specs...) - ancestor = ancestor.parent - } - // write the list of global options if len(globals) > 0 { fmt.Fprint(w, "\nGlobal options:\n") for _, spec := range globals { p.printOption(w, spec) - if spec.long == "version" { - hasVersionOption = true - } } } |
