Skip to content

Commit

Permalink
Fix for windows long/short names
Browse files Browse the repository at this point in the history
  • Loading branch information
jkieboom committed Jun 17, 2024
1 parent ef0e815 commit 58135da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
15 changes: 13 additions & 2 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,21 @@ func (c *completion) completeOptionNames(s *parseState, prefix string, match str
var results []Completion
repeats := map[string]bool{}

var longprefix string
var shortprefix string

if prefix == "/" {
longprefix = "/"
shortprefix = "/"
} else {
longprefix = "--"
shortprefix = "-"
}

for name, opt := range s.lookup.longNames {
if strings.HasPrefix(name, match) && !opt.Hidden {
results = append(results, Completion{
Item: defaultLongOptDelimiter + name,
Item: longprefix + name,
Description: opt.Description,
})

Expand All @@ -108,7 +119,7 @@ func (c *completion) completeOptionNames(s *parseState, prefix string, match str
for name, opt := range s.lookup.shortNames {
if _, exist := repeats[name]; !exist && strings.HasPrefix(name, match) && !opt.Hidden {
results = append(results, Completion{
Item: string(defaultShortOptDelimiter) + name,
Item: shortprefix + name,
Description: opt.Description,
})
}
Expand Down
46 changes: 29 additions & 17 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -81,6 +82,14 @@ type completionTest struct {

var completionTests []completionTest

func makeLongName(option string) string {
return defaultLongOptDelimiter + option
}

func makeShortName(option string) string {
return string(defaultShortOptDelimiter) + option
}

func init() {
_, sourcefile, _, _ := runtime.Caller(0)
completionTestSourcedir := filepath.Join(filepath.SplitList(path.Dir(sourcefile))...)
Expand All @@ -97,15 +106,15 @@ func init() {
completionTests = []completionTest{
{
// Short names
[]string{"-"},
[]string{"--debug", "--required", "--verbose", "--version", "-i"},
[]string{makeShortName("")},
[]string{makeLongName("debug"), makeLongName("required"), makeLongName("verbose"), makeLongName("version"), makeShortName("i")},
false,
},

{
// Short names full
[]string{"-i"},
[]string{"-i"},
[]string{makeShortName("i")},
[]string{makeShortName("i")},
false,
},

Expand All @@ -125,20 +134,20 @@ func init() {

{
// Long names with descriptions
[]string{"--"},
[]string{makeLongName("")},
[]string{
"--debug # Enable debug",
"--required # This is required",
"--verbose # Verbose messages",
"--version # Show version",
makeLongName("debug # Enable debug"),
makeLongName("required # This is required"),
makeLongName("verbose # Verbose messages"),
makeLongName("version # Show version"),
},
true,
},

{
// Long names partial
[]string{"--ver"},
[]string{"--verbose", "--version"},
[]string{makeLongName("ver")},
[]string{makeLongName("verbose"), makeLongName("version")},
false,
},

Expand Down Expand Up @@ -197,7 +206,7 @@ func init() {

{
// Flag filename
[]string{"rm", "-f", path.Join(completionTestSourcedir, "completion")},
[]string{"rm", makeShortName("f"), path.Join(completionTestSourcedir, "completion")},
completionTestFilename,
false,
},
Expand All @@ -212,21 +221,21 @@ func init() {
{
// Flag concat filename
[]string{"rm", "-f" + path.Join(completionTestSourcedir, "completion")},
[]string{"-f" + completionTestFilename[0], "-f" + completionTestFilename[1]},
[]string{"-f" + completionTestFilename[0], makeShortName("f") + completionTestFilename[1]},
false,
},

{
// Flag equal concat filename
[]string{"rm", "-f=" + path.Join(completionTestSourcedir, "completion")},
[]string{"-f=" + completionTestFilename[0], "-f=" + completionTestFilename[1]},
[]string{"-f=" + completionTestFilename[0], makeShortName("f=") + completionTestFilename[1]},
false,
},

{
// Flag concat long filename
[]string{"rm", "--filename=" + path.Join(completionTestSourcedir, "completion")},
[]string{"--filename=" + completionTestFilename[0], "--filename=" + completionTestFilename[1]},
[]string{"--filename=" + completionTestFilename[0], makeLongName("filename=") + completionTestFilename[1]},
false,
},

Expand All @@ -253,13 +262,13 @@ func init() {

{
// Custom completed
[]string{"rename", "-c", "hello un"},
[]string{"rename", makeShortName("c"), "hello un"},
[]string{"hello universe"},
false,
},
{
// Multiple flag filename
[]string{"add-multi-flag", "-f", filepath.Join(completionTestSourcedir, "completion")},
[]string{"add-multi-flag", makeShortName("f"), filepath.Join(completionTestSourcedir, "completion")},
completionTestFilename,
false,
},
Expand All @@ -282,6 +291,9 @@ func TestCompletion(t *testing.T) {
items[i] = v.Item
}

sort.Strings(items)
sort.Strings(test.Completed)

if !reflect.DeepEqual(items, test.Completed) {
t.Errorf("Args: %#v, %#v\n Expected: %#v\n Got: %#v", test.Args, test.ShowDescriptions, test.Completed, items)
}
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestDefaults(t *testing.T) {
{
msg: "non-zero value arguments, expecting overwritten arguments",
args: []string{"-3=true"},
expectedErr: "bool flag `-3' cannot have an argument",
expectedErr: "bool flag `" + makeShortName("3") + "' cannot have an argument",
},
{
msg: "zero value arguments, expecting overwritten arguments",
Expand Down

0 comments on commit 58135da

Please sign in to comment.