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

fix: command.Names should not contain empty strings when command has no name #15

Merged
merged 1 commit into from
Jun 14, 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
19 changes: 12 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,12 @@ func (c *Command) FullName() string {

func (c *Command) PreferredName() string {
name := c.FullName()
if len(c.Names()) > 1 {
// first alias is what we prefer
name = c.Names()[1]
}
if name == "" && len(c.Aliases) > 0 {
names := []string{}
for _, a := range c.Aliases {
names = append(names, a.String())
if name := a.String(); name != "" {
names = append(names, a.String())
}
}
return strings.Join(names, ", ")
}
Expand Down Expand Up @@ -170,12 +168,19 @@ func (c *Command) Names() []string {
if c.Category != "" {
name = c.Category + ":" + name
}
names := []string{name}
names := []string{}
if name != "" {
names = append(names, name)
}
for _, a := range c.Aliases {
if !a.Hidden {
if a.Hidden {
continue
}
if name := a.String(); name != "" {
names = append(names, a.String())
}
}

return names
}

Expand Down
21 changes: 21 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"flag"
"io"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -146,3 +147,23 @@ func TestFuzzyCommandNames(t *testing.T) {
t.Fatalf("expected no matches, got %v", c)
}
}

func TestCommandWithNoNames(t *testing.T) {
c := Command{
Aliases: []*Alias{
{},
{Name: "foo"},
{Name: "bar"},
},
}

if got, expected := c.Names(), []string{"foo", "bar"}; len(got) != 2 {
t.Fatalf("expected two names, got %v", len(got))
} else if !reflect.DeepEqual(got, expected) {
t.Fatalf("expected %v, got %v", expected, got)
}

if name := c.PreferredName(); name != "foo, bar" {
t.Fatalf(`expected "foo, bar", got "%v"`, name)
}
}
Loading