summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go27
1 files changed, 5 insertions, 22 deletions
diff --git a/usage.go b/usage.go
index 66a5be9..b84aa10 100644
--- a/usage.go
+++ b/usage.go
@@ -48,39 +48,17 @@ 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)
}
}
- // 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)
- }
-
// print the beginning of the usage string
fmt.Fprintf(w, "Usage: %s", p.cmd.name)
for _, s := range subcommand {
@@ -254,6 +232,11 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error
if p.description != "" {
fmt.Fprintln(w, p.description)
}
+
+ if !hasVersionOption && p.version != "" {
+ fmt.Fprintln(w, p.version)
+ }
+
p.WriteUsageForSubcommand(w, subcommand...)
// write the list of positionals