diff options
| author | Alex Flint <[email protected]> | 2024-09-05 17:19:12 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-09-05 17:19:12 -0400 |
| commit | a5045bbe852cb542c8fab73aada516dbb32c7f2e (patch) | |
| tree | 8855b562d6b7782fa285d1d45679b2e98cd3f445 | |
| parent | 3925edf11a926d46a241b837e0712093ecc3f174 (diff) | |
| parent | 3673177bf97072ef223db831b659d2d66cc54a58 (diff) | |
Merge pull request #259 from hhromic/fix-193
Move writing program version from usage to help writer
| -rw-r--r-- | usage.go | 27 | ||||
| -rw-r--r-- | usage_test.go | 6 |
2 files changed, 8 insertions, 25 deletions
@@ -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 diff --git a/usage_test.go b/usage_test.go index a958abb..fc8aaff 100644 --- a/usage_test.go +++ b/usage_test.go @@ -237,7 +237,7 @@ func (versioned) Version() string { } func TestUsageWithVersion(t *testing.T) { - expectedUsage := "example 3.2.1\nUsage: example" + expectedUsage := "Usage: example" expectedHelp := ` example 3.2.1 @@ -322,7 +322,7 @@ type subcommand struct { } func TestUsageWithVersionAndSubcommand(t *testing.T) { - expectedUsage := "example 3.2.1\nUsage: example <command> [<args>]" + expectedUsage := "Usage: example <command> [<args>]" expectedHelp := ` example 3.2.1 @@ -353,7 +353,7 @@ Commands: p.WriteUsage(&usage) assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) - expectedUsage = "example 3.2.1\nUsage: example cmd [--number NUMBER]" + expectedUsage = "Usage: example cmd [--number NUMBER]" expectedHelp = ` example 3.2.1 |
