Compare commits

...

2 Commits

Author SHA1 Message Date
dependabot[bot] 4fa4fdf5cd
build(deps): bump github.com/inconshreveable/mousetrap (#1872) 2023-01-03 02:43:06 +00:00
Dominik Roos b4f979ae35
completions: do not detect arguments with dash as 2nd char as flag (#1817)
Fixes #1816

Previously, arguments with a dash as the second character (e.g., 1-ff00:0:1)
were detected as a flag by mistake. This resulted in auto completion misbehaving
if such an argument was last in the argument list during invocation.
2023-01-02 19:58:36 -05:00
4 changed files with 68 additions and 4 deletions

View File

@ -692,7 +692,7 @@ Loop:
}
func isFlagArg(arg string) bool {
return ((len(arg) >= 3 && arg[1] == '-') ||
return ((len(arg) >= 3 && arg[0:2] == "--") ||
(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
}

View File

@ -3115,3 +3115,67 @@ func TestCompletionCobraFlags(t *testing.T) {
})
}
}
func TestArgsNotDetectedAsFlagsCompletionInGo(t *testing.T) {
// Regression test that ensures the bug described in
// https://github.com/spf13/cobra/issues/1816 does not occur anymore.
root := Command{
Use: "root",
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
return []string{"service", "1-123", "11-123"}, ShellCompDirectiveNoFileComp
},
}
completion := `service
1-123
11-123
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
`
testcases := []struct {
desc string
args []string
expectedOutput string
}{
{
desc: "empty",
args: []string{""},
expectedOutput: completion,
},
{
desc: "service only",
args: []string{"service", ""},
expectedOutput: completion,
},
{
desc: "service last",
args: []string{"1-123", "service", ""},
expectedOutput: completion,
},
{
desc: "two digit prefixed dash last",
args: []string{"service", "11-123", ""},
expectedOutput: completion,
},
{
desc: "one digit prefixed dash last",
args: []string{"service", "1-123", ""},
expectedOutput: completion,
},
}
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
args := []string{ShellCompNoDescRequestCmd}
args = append(args, tc.args...)
output, err := executeCommand(&root, args...)
switch {
case err == nil && output != tc.expectedOutput:
t.Errorf("expected: %q, got: %q", tc.expectedOutput, output)
case err != nil:
t.Errorf("Unexpected error %q", err)
}
})
}
}

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.15
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2
github.com/inconshreveable/mousetrap v1.0.1
github.com/inconshreveable/mousetrap v1.1.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
)

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=