Move writing program version from usage to help writer

* Writing the version on usage text is unexpected and confusing
This commit is contained in:
Hugo Hromic 2024-06-30 23:36:55 +01:00
parent 3de7278c4f
commit 3673177bf9
No known key found for this signature in database
GPG Key ID: 5CAFA8074D3F443B
2 changed files with 8 additions and 25 deletions

View File

@ -48,39 +48,17 @@ func (p *Parser) WriteUsageForSubcommand(w io.Writer, subcommand ...string) erro
} }
var positionals, longOptions, shortOptions []*spec var positionals, longOptions, shortOptions []*spec
var hasVersionOption bool
for _, spec := range cmd.specs { for _, spec := range cmd.specs {
switch { switch {
case spec.positional: case spec.positional:
positionals = append(positionals, spec) positionals = append(positionals, spec)
case spec.long != "": case spec.long != "":
longOptions = append(longOptions, spec) longOptions = append(longOptions, spec)
if spec.long == "version" {
hasVersionOption = true
}
case spec.short != "": case spec.short != "":
shortOptions = append(shortOptions, spec) 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 // print the beginning of the usage string
fmt.Fprintf(w, "Usage: %s", p.cmd.name) fmt.Fprintf(w, "Usage: %s", p.cmd.name)
for _, s := range subcommand { for _, s := range subcommand {
@ -254,6 +232,11 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error
if p.description != "" { if p.description != "" {
fmt.Fprintln(w, p.description) fmt.Fprintln(w, p.description)
} }
if !hasVersionOption && p.version != "" {
fmt.Fprintln(w, p.version)
}
p.WriteUsageForSubcommand(w, subcommand...) p.WriteUsageForSubcommand(w, subcommand...)
// write the list of positionals // write the list of positionals

View File

@ -237,7 +237,7 @@ func (versioned) Version() string {
} }
func TestUsageWithVersion(t *testing.T) { func TestUsageWithVersion(t *testing.T) {
expectedUsage := "example 3.2.1\nUsage: example" expectedUsage := "Usage: example"
expectedHelp := ` expectedHelp := `
example 3.2.1 example 3.2.1
@ -322,7 +322,7 @@ type subcommand struct {
} }
func TestUsageWithVersionAndSubcommand(t *testing.T) { func TestUsageWithVersionAndSubcommand(t *testing.T) {
expectedUsage := "example 3.2.1\nUsage: example <command> [<args>]" expectedUsage := "Usage: example <command> [<args>]"
expectedHelp := ` expectedHelp := `
example 3.2.1 example 3.2.1
@ -353,7 +353,7 @@ Commands:
p.WriteUsage(&usage) p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) 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 = ` expectedHelp = `
example 3.2.1 example 3.2.1