Skip to content

feat: make commands names case insensitive #21

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

Merged
merged 1 commit into from
Feb 17, 2025
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
4 changes: 4 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"sort"
"strings"
"sync"
"time"

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

// Category returns the named CommandCategory on App. Returns nil if the category does not exist
func (a *Application) Category(name string) *CommandCategory {
name = strings.ToLower(name)
if a.Categories == nil {
return nil
}
Expand Down Expand Up @@ -315,6 +318,7 @@ func (a *Application) setup() {
registerAutocompleteCommands(a)

for _, c := range a.Commands {
c.normalizeCommandNames()
if c.HelpName == "" {
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.FullName())
}
Expand Down
9 changes: 9 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func Hide() bool {
return true
}

func (c *Command) normalizeCommandNames() {
c.Category = strings.ToLower(c.Category)
c.Name = strings.ToLower(c.Name)
c.HelpName = strings.ToLower(c.HelpName)
for _, alias := range c.Aliases {
alias.Name = strings.ToLower(alias.Name)
}
}

// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c *Command) FullName() string {
Expand Down
32 changes: 32 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
}
}

func TestCaseInsensitiveCommandNames(t *testing.T) {
app := Application{}
app.ErrWriter = io.Discard
projectList := &Command{Name: "project:LIST", Aliases: []*Alias{{Name: "FOO"}}}
projectLink := &Command{Name: "PROJECT:link"}
app.Commands = []*Command{
projectList,
projectLink,
}

app.setup()

if c := app.BestCommand("project:list"); c != projectList {
t.Fatalf("expected project:list, got %v", c)
}
if c := app.BestCommand("Project:lISt"); c != projectList {
t.Fatalf("expected project:list, got %v", c)
}
if c := app.BestCommand("project:link"); c != projectLink {
t.Fatalf("expected project:link, got %v", c)
}
if c := app.BestCommand("project:Link"); c != projectLink {
t.Fatalf("expected project:link, got %v", c)
}
if c := app.BestCommand("foo"); c != projectList {
t.Fatalf("expected project:link, got %v", c)
}
if c := app.BestCommand("FoO"); c != projectList {
t.Fatalf("expected project:link, got %v", c)
}
}

func TestFuzzyCommandNames(t *testing.T) {
app := Application{}
app.ErrWriter = io.Discard
Expand Down
Loading