From 0c125f4ffedaa3c53c6b092c3b3b1f2b9e1c227b Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 17 Jun 2022 00:26:37 +0500 Subject: [PATCH] temp commit --- completions.go | 39 ++++++- completions_test.go | 277 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+), 2 deletions(-) diff --git a/completions.go b/completions.go index 3de4e87a..ba0eac0a 100644 --- a/completions.go +++ b/completions.go @@ -259,6 +259,7 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi // remove the flag name argument from the list of finalArgs or else the parsing // could fail due to an invalid value (incomplete) for the flag. flag, finalArgs, toComplete, flagErr := checkIfFlagCompletion(finalCmd, finalArgs, toComplete) + fmt.Println(flag, finalArgs, toComplete) // Check if interspersed is false or -- was set on a previous arg. // This works by counting the arguments. Normally -- is not counted as arg but @@ -512,6 +513,10 @@ func completeRequireFlags(finalCmd *Command, toComplete string) []string { return completions } +func flagsNoSpace() { + +} + func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*pflag.Flag, []string, string, error) { if finalCmd.DisableFlagParsing { // We only do flag completion if we are allowed to parse flags @@ -543,7 +548,36 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p lastArg = lastArg[index+1:] flagWithEqual = true } else { - // Normal flag completion + + // Flag with full name + //if strings.HasPrefix(lastArg, "--") { + // // Normal flag completion + // return nil, args, orgLastArg, nil + //} + // + //i := 1 + // + //for ; i < len(lastArg)-1; i++ { + // flagName = lastArg[i : i+1] + // f := findFlag(finalCmd, flagName) + // if f != nil && f.Value.Type() != "bool" { + // flagName = lastArg[i : i+1] + // lastArg = lastArg[i+1:] + // i = -1 + // break + // } + //} + // + //if i == len(orgLastArg)-1 { + // return nil, args, orgLastArg, nil + //} + // + //fmt.Printf("flagname: %s, lastArg: %s\n", flagName, lastArg) + //if i != -1 { + // // Normal flag completion + // return nil, args, orgLastArg, nil + //} + //// Normal flag completion return nil, args, lastArg, nil } } @@ -595,7 +629,8 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p } } - return flag, trimmedArgs, lastArg, nil + return flag, []string{"-it"}, lastArg, nil + //return flag, trimmedArgs, lastArg, nil } // initDefaultCompletionCmd adds a default 'completion' command to c. diff --git a/completions_test.go b/completions_test.go index 28a3228e..afe861b9 100644 --- a/completions_test.go +++ b/completions_test.go @@ -3,6 +3,7 @@ package cobra import ( "bytes" "context" + "fmt" "strings" "testing" ) @@ -2691,3 +2692,279 @@ func TestFixedCompletions(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } } + +func TestMyShorthandFlagCompletionInGoWithDesc(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + rootCmd.Flags().StringP("inter", "i", "", "first flag") + rootCmd.Flags().StringP("temp", "t", "", "second flag") + rootCmd.Flags().StringP("name", "n", "", "third flag") + + _ = rootCmd.RegisterFlagCompletionFunc("inter", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + return []string{"myval1", "mywal", "myvlav"}, ShellCompDirectiveDefault + }) + + // Test that flag names are completed + output, err := executeCommand(rootCmd, ShellCompRequestCmd, "-i") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := strings.Join([]string{ + "-i\tfirst flag", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("\nER: %q\nAR: %q", expected, output) + } + +} + +func TestNoSpace(t *testing.T) { + rootCmd := &Command{ + Use: "root", + Run: emptyRun, + } + + f := rootCmd.Flags() + f.BoolP("interactive", "i", false, "short flag 1") + f.BoolP("timeout", "t", false, "short flag 2") + f.StringP("namespace", "n", "defValue", "file namespace") + _ = rootCmd.RegisterFlagCompletionFunc("short3", func(*Command, []string, string) ([]string, ShellCompDirective) { + return []string{"works"}, ShellCompDirectiveNoFileComp + }) + + namespaceComps := []string{"infra-production1", "infra-production2", "prod-infra-production1"} + + _ = rootCmd.RegisterFlagCompletionFunc("namespace", func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + comps := make([]string, 0, len(namespaceComps)) + for _, comp := range namespaceComps { + if strings.HasPrefix(comp, toComplete) { + comps = append(comps, comp) + } + } + if len(comps) == 0 { + comps = namespaceComps + } + return comps, ShellCompDirectiveNoFileComp + }) + + // Test that multiple boolean + string with equal sign with value shorthand flags work + output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-itn", "i") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + fmt.Printf("interactive: %v\n", f.Lookup("interactive").Changed) + fmt.Printf("timeout: %v\n", f.Lookup("timeout").Changed) + + expected := strings.Join([]string{ + "-n", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("\nER: %q\nAR: %q", expected, output) + } + + // Test that multiple boolean + string with equal sign with value shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-it") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "infra-production1", + "infra-production2", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple boolean + string with equal sign with value shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-n", "") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "-n", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple boolean + string with equal sign with value shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-inp") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + "prod-infra-production1", + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + // Test that multiple boolean + string with equal sign with value shorthand flags work + output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-pnp") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = strings.Join([]string{ + ":4", + "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + + if output != expected { + t.Errorf("expected: %q, got: %q", expected, output) + } + + //// Test that multiple boolean + string with equal sign with value shorthand flags work + //output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-f", "wor") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + //} + // + //expected := strings.Join([]string{ + // //"anp", + // "bnp1", + // //"bnp2", + // //"cynp1", + // ":4", + // "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + // + //fmt.Println(f.Lookup("short").Changed) + // + //if output != expected { + // t.Errorf("expected: %q, got: %q", expected, output) + //} + // + //// Test that multiple boolean + string with equal sign with value shorthand flags work + //output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-n", "b") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + //} + // + //expected = strings.Join([]string{ + // //"anp", + // "bnp1", + // "bnp2", + // //"cynp1", + // ":4", + // "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + // + //if output != expected { + // t.Errorf("expected: %q, got: %q", expected, output) + //} + // + //// Test that multiple boolean + string with equal sign with value shorthand flags work + //output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-n=b") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + //} + // + //expected = strings.Join([]string{ + // //"anp", + // "bnp1", + // "bnp2", + // //"cynp1", + // ":4", + // "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") + // + //if output != expected { + // t.Errorf("expected: %q, got: %q", expected, output) + //} + // + ////-------------------------------- + // + //// 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) + //} + // + //// Test that multiple boolean + string with equal sign shorthand flags work + //output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=wo") + //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) + //} + // + //// Test that multiple boolean + string with equal sign with value shorthand flags work + //output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=abc", "") + //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) + //} + +}