Skip to content

Commit 88623b9

Browse files
committed
feat: make commands names case insensitive
1 parent 82bea43 commit 88623b9

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

application.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"sort"
26+
"strings"
2627
"sync"
2728
"time"
2829

@@ -168,6 +169,7 @@ func (a *Application) Command(name string) *Command {
168169
// there is only one. Returns nil if the command does not exist of if the fuzzy
169170
// matching find more than one.
170171
func (a *Application) BestCommand(name string) *Command {
172+
name = strings.ToLower(name)
171173
if c := a.Command(name); c != nil {
172174
return c
173175
}
@@ -188,6 +190,7 @@ func (a *Application) BestCommand(name string) *Command {
188190

189191
// Category returns the named CommandCategory on App. Returns nil if the category does not exist
190192
func (a *Application) Category(name string) *CommandCategory {
193+
name = strings.ToLower(name)
191194
if a.Categories == nil {
192195
return nil
193196
}
@@ -315,6 +318,7 @@ func (a *Application) setup() {
315318
registerAutocompleteCommands(a)
316319

317320
for _, c := range a.Commands {
321+
c.normalizeCommandNames()
318322
if c.HelpName == "" {
319323
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.FullName())
320324
}

command.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ func Hide() bool {
7676
return true
7777
}
7878

79+
func (c *Command) normalizeCommandNames() {
80+
c.Category = strings.ToLower(c.Category)
81+
c.Name = strings.ToLower(c.Name)
82+
c.HelpName = strings.ToLower(c.HelpName)
83+
for _, alias := range c.Aliases {
84+
alias.Name = strings.ToLower(alias.Name)
85+
}
86+
}
87+
7988
// FullName returns the full name of the command.
8089
// For subcommands this ensures that parent commands are part of the command path
8190
func (c *Command) FullName() string {

command_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,38 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
112112
}
113113
}
114114

115+
func TestCaseInsensitiveCommandNames(t *testing.T) {
116+
app := Application{}
117+
app.ErrWriter = io.Discard
118+
projectList := &Command{Name: "project:LIST", Aliases: []*Alias{{Name: "FOO"}}}
119+
projectLink := &Command{Name: "PROJECT:link"}
120+
app.Commands = []*Command{
121+
projectList,
122+
projectLink,
123+
}
124+
125+
app.setup()
126+
127+
if c := app.BestCommand("project:list"); c != projectList {
128+
t.Fatalf("expected project:list, got %v", c)
129+
}
130+
if c := app.BestCommand("Project:lISt"); c != projectList {
131+
t.Fatalf("expected project:list, got %v", c)
132+
}
133+
if c := app.BestCommand("project:link"); c != projectLink {
134+
t.Fatalf("expected project:link, got %v", c)
135+
}
136+
if c := app.BestCommand("project:Link"); c != projectLink {
137+
t.Fatalf("expected project:link, got %v", c)
138+
}
139+
if c := app.BestCommand("foo"); c != projectList {
140+
t.Fatalf("expected project:link, got %v", c)
141+
}
142+
if c := app.BestCommand("FoO"); c != projectList {
143+
t.Fatalf("expected project:link, got %v", c)
144+
}
145+
}
146+
115147
func TestFuzzyCommandNames(t *testing.T) {
116148
app := Application{}
117149
app.ErrWriter = io.Discard

0 commit comments

Comments
 (0)