summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2024-06-30 12:27:39 -0400
committerGitHub <[email protected]>2024-06-30 12:27:39 -0400
commit0cc152dce52a7a61cc3cd73d25eae82adad99807 (patch)
treee0b101002f87ab9c708f539ce2b9034ca1c1cca1 /usage.go
parentb6422dcbc3ca07c96adfcab27e1f6b9a7af93160 (diff)
parentc087d7180231ea3cfc12a79ee091786ac9954e6a (diff)
Merge pull request #224 from hhromic/better-version-v2
Fix usage writing when using custom version flag
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go52
1 files changed, 37 insertions, 15 deletions
diff --git a/usage.go b/usage.go
index f5e4b38..66a5be9 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)
}
@@ -208,6 +226,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 +236,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 +272,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
- }
}
}