mirror of
https://github.com/spf13/cobra
synced 2025-05-05 04:47:22 +00:00
temp commit
This commit is contained in:
parent
87ea1807f7
commit
0c125f4ffe
2 changed files with 314 additions and 2 deletions
|
@ -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
|
// 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.
|
// could fail due to an invalid value (incomplete) for the flag.
|
||||||
flag, finalArgs, toComplete, flagErr := checkIfFlagCompletion(finalCmd, finalArgs, toComplete)
|
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.
|
// 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
|
// 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
|
return completions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func flagsNoSpace() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*pflag.Flag, []string, string, error) {
|
func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*pflag.Flag, []string, string, error) {
|
||||||
if finalCmd.DisableFlagParsing {
|
if finalCmd.DisableFlagParsing {
|
||||||
// We only do flag completion if we are allowed to parse flags
|
// 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:]
|
lastArg = lastArg[index+1:]
|
||||||
flagWithEqual = true
|
flagWithEqual = true
|
||||||
} else {
|
} 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
|
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.
|
// initDefaultCompletionCmd adds a default 'completion' command to c.
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -2691,3 +2692,279 @@ func TestFixedCompletions(t *testing.T) {
|
||||||
t.Errorf("expected: %q, got: %q", expected, output)
|
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)
|
||||||
|
//}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue