From 58135da7bdd27631b4c5f5daff5c2deef2c0f33b Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Mon, 17 Jun 2024 08:11:12 +0200 Subject: [PATCH] Fix for windows long/short names --- completion.go | 15 +++++++++++++-- completion_test.go | 46 +++++++++++++++++++++++++++++----------------- parser_test.go | 2 +- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/completion.go b/completion.go index 8ed61f1..d11a6fa 100644 --- a/completion.go +++ b/completion.go @@ -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, }) @@ -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, }) } diff --git a/completion_test.go b/completion_test.go index aa3af90..eafd3de 100644 --- a/completion_test.go +++ b/completion_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "reflect" "runtime" + "sort" "strings" "testing" ) @@ -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))...) @@ -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, }, @@ -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, }, @@ -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, }, @@ -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, }, @@ -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, }, @@ -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) } diff --git a/parser_test.go b/parser_test.go index df37044..e675cfd 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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",