summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorHugo Hromic <[email protected]>2024-01-18 23:04:55 +0000
committerHugo Hromic <[email protected]>2024-06-29 23:42:22 +0100
commitbed89eb683e6016be7247041db3c998e57fc838c (patch)
treeec76200400a92602d3d9b201e41e6f2688e87ca3 /usage.go
parent4ed4ce751fa49ae1b04f672c1092120fd71d5b21 (diff)
Implement scanning of version flag in specs for usage generation
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go20
1 files changed, 19 insertions, 1 deletions
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)
}