mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Merge upstream into zsh-completion
This commit is contained in:
commit
c99a50b65c
2 changed files with 22 additions and 2 deletions
|
@ -317,7 +317,7 @@ func writeFlagHandler(buf *bytes.Buffer, name string, annotations map[string][]s
|
||||||
|
|
||||||
var ext string
|
var ext string
|
||||||
if len(value) > 0 {
|
if len(value) > 0 {
|
||||||
ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Name()) + strings.Join(value, "|")
|
ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Root().Name()) + strings.Join(value, "|")
|
||||||
} else {
|
} else {
|
||||||
ext = "_filedir"
|
ext = "_filedir"
|
||||||
}
|
}
|
||||||
|
@ -335,7 +335,7 @@ func writeFlagHandler(buf *bytes.Buffer, name string, annotations map[string][]s
|
||||||
|
|
||||||
var ext string
|
var ext string
|
||||||
if len(value) == 1 {
|
if len(value) == 1 {
|
||||||
ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Name()) + value[0]
|
ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Root().Name()) + value[0]
|
||||||
} else {
|
} else {
|
||||||
ext = "_filedir -d"
|
ext = "_filedir -d"
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -21,6 +22,16 @@ func check(t *testing.T, found, expected string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkRegex(t *testing.T, found, pattern string) {
|
||||||
|
matched, err := regexp.MatchString(pattern, found)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error thrown performing MatchString: \n %s\n", err)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("Expecting to match: \n %q\nGot:\n %q\n", pattern, found)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runShellCheck(s string) error {
|
func runShellCheck(s string) error {
|
||||||
excluded := []string{
|
excluded := []string{
|
||||||
"SC2034", // PREFIX appears unused. Verify it or export it.
|
"SC2034", // PREFIX appears unused. Verify it or export it.
|
||||||
|
@ -86,6 +97,11 @@ func TestBashCompletions(t *testing.T) {
|
||||||
Run: emptyRun,
|
Run: emptyRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echoCmd.Flags().String("filename", "", "Enter a filename")
|
||||||
|
echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
|
||||||
|
echoCmd.Flags().String("config", "", "config to use (located in /config/PROFILE/)")
|
||||||
|
echoCmd.Flags().SetAnnotation("config", BashCompSubdirsInDir, []string{"config"})
|
||||||
|
|
||||||
printCmd := &Command{
|
printCmd := &Command{
|
||||||
Use: "print [string to print]",
|
Use: "print [string to print]",
|
||||||
Args: MinimumNArgs(1),
|
Args: MinimumNArgs(1),
|
||||||
|
@ -148,10 +164,14 @@ func TestBashCompletions(t *testing.T) {
|
||||||
check(t, output, `must_have_one_noun+=("three")`)
|
check(t, output, `must_have_one_noun+=("three")`)
|
||||||
// check for filename extension flags
|
// check for filename extension flags
|
||||||
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_filename_extension_flag json|yaml|yml")`, rootCmd.Name()))
|
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_filename_extension_flag json|yaml|yml")`, rootCmd.Name()))
|
||||||
|
// check for filename extension flags in a subcommand
|
||||||
|
checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_filename_extension_flag json\|yaml\|yml"\)`, rootCmd.Name()))
|
||||||
// check for custom flags
|
// check for custom flags
|
||||||
check(t, output, `flags_completion+=("__complete_custom")`)
|
check(t, output, `flags_completion+=("__complete_custom")`)
|
||||||
// check for subdirs_in_dir flags
|
// check for subdirs_in_dir flags
|
||||||
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_subdirs_in_dir_flag themes")`, rootCmd.Name()))
|
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_subdirs_in_dir_flag themes")`, rootCmd.Name()))
|
||||||
|
// check for subdirs_in_dir flags in a subcommand
|
||||||
|
checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_subdirs_in_dir_flag config"\)`, rootCmd.Name()))
|
||||||
|
|
||||||
checkOmit(t, output, deprecatedCmd.Name())
|
checkOmit(t, output, deprecatedCmd.Name())
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue