From df56fef0d267b6e790b64a33252180115080cc83 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 15 Oct 2020 13:10:34 +0200 Subject: [PATCH] Custom completion handle multiple shorhand flags together Flag definitions like `-asd` are not handled correctly by the custom completion logic. They should be treated as multiple flags. For details refer to #1257. Fixes #1257 Signed-off-by: Paul Holzinger --- custom_completions.go | 7 +++-- custom_completions_test.go | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/custom_completions.go b/custom_completions.go index f9e88e081..05937631f 100644 --- a/custom_completions.go +++ b/custom_completions.go @@ -459,8 +459,11 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p // If the flag contains an = it means it has already been fully processed, // so we don't need to deal with it here. if index := strings.Index(prevArg, "="); index < 0 { - flagName = strings.TrimLeft(prevArg, "-") - + if strings.HasPrefix(prevArg, "--") { + flagName = prevArg[2:] + } else { + flagName = prevArg[len(prevArg)-1:] + } // Remove the uncompleted flag or else there could be an error created // for an invalid value for that flag trimmedArgs = args[:len(args)-1] diff --git a/custom_completions_test.go b/custom_completions_test.go index 14ec5a9eb..450a0e18a 100644 --- a/custom_completions_test.go +++ b/custom_completions_test.go @@ -1958,3 +1958,65 @@ func TestCompleteHelp(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } } + +func TestMultipleShorthandFlagCompletion(t *testing.T) { + rootCmd := &Command{ + Use: "root", + ValidArgs: []string{"foo", "bar"}, + Run: emptyRun, + } + f := rootCmd.Flags() + f.BoolP("short", "s", false, "short flag 1") + f.BoolP("short2", "d", false, "short flag 2") + f.StringP("short3", "f", "", "short flag 3") + rootCmd.RegisterFlagCompletionFunc("short3", func(*Command, []string, string) ([]string, ShellCompDirective) { + return []string{"works"}, ShellCompDirectiveNoFileComp + }) + + // Test that a single shorthand flag works + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := strings.Join([]string{ + "foo", + "bar", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple boolean shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sd", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "foo", + "bar", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple boolean + string shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "works", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } +}