From bed89eb683e6016be7247041db3c998e57fc838c Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Thu, 18 Jan 2024 23:04:55 +0000 Subject: [PATCH] Implement scanning of version flag in specs for usage generation --- usage.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/usage.go b/usage.go index ae98478..4a056f3 100644 --- a/usage.go +++ b/usage.go @@ -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) }