Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move writing program version from usage to help writer #259

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading