diff options
| author | Hugo Hromic <[email protected]> | 2024-01-18 23:04:55 +0000 |
|---|---|---|
| committer | Hugo Hromic <[email protected]> | 2024-06-29 23:42:22 +0100 |
| commit | bed89eb683e6016be7247041db3c998e57fc838c (patch) | |
| tree | ec76200400a92602d3d9b201e41e6f2688e87ca3 /usage.go | |
| parent | 4ed4ce751fa49ae1b04f672c1092120fd71d5b21 (diff) | |
Implement scanning of version flag in specs for usage generation
Diffstat (limited to 'usage.go')
| -rw-r--r-- | usage.go | 20 |
1 files changed, 19 insertions, 1 deletions
@@ -48,18 +48,36 @@ func (p *Parser) WriteUsageForSubcommand(w io.Writer, subcommand ...string) erro } var positionals, longOptions, shortOptions []*spec + var hasVersionOption bool for _, spec := range cmd.specs { switch { case spec.positional: positionals = append(positionals, spec) case spec.long != "": longOptions = append(longOptions, spec) + if spec.long == "version" { + hasVersionOption = true + } case spec.short != "": shortOptions = append(shortOptions, spec) } } - if p.version != "" { + // make a list of ancestor commands so that we print with full context + // also determine if any ancestor has a version option spec + var ancestors []string + ancestor := cmd + for ancestor != nil { + for _, spec := range ancestor.specs { + if spec.long == "version" { + hasVersionOption = true + } + } + ancestors = append(ancestors, ancestor.name) + ancestor = ancestor.parent + } + + if !hasVersionOption && p.version != "" { fmt.Fprintln(w, p.version) } |
