forked from mirror/cobra
Merge pull request #126 from anthonyfok/bash-completion/subdirs-in-dir
Add new BashCompSubdirsInDir annotation
This commit is contained in:
commit
c55cdf3385
|
@ -13,6 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extentions"
|
BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extentions"
|
||||||
BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
|
BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
|
||||||
|
BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"
|
||||||
)
|
)
|
||||||
|
|
||||||
func preamble(out *bytes.Buffer) {
|
func preamble(out *bytes.Buffer) {
|
||||||
|
@ -100,6 +101,12 @@ __handle_filename_extension_flag()
|
||||||
_filedir "@(${ext})"
|
_filedir "@(${ext})"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__handle_subdirs_in_dir_flag()
|
||||||
|
{
|
||||||
|
local dir="$1"
|
||||||
|
pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
__handle_flag()
|
__handle_flag()
|
||||||
{
|
{
|
||||||
__debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
|
__debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
|
||||||
|
@ -226,6 +233,16 @@ func writeFlagHandler(name string, annotations map[string][]string, out *bytes.B
|
||||||
ext := "_filedir"
|
ext := "_filedir"
|
||||||
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||||
}
|
}
|
||||||
|
case BashCompSubdirsInDir:
|
||||||
|
fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)
|
||||||
|
|
||||||
|
if len(value) == 1 {
|
||||||
|
ext := "__handle_subdirs_in_dir_flag " + value[0]
|
||||||
|
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||||
|
} else {
|
||||||
|
ext := "_filedir -d"
|
||||||
|
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,11 @@ func TestBashCompletions(t *testing.T) {
|
||||||
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
||||||
c.MarkFlagFilename("filename-ext")
|
c.MarkFlagFilename("filename-ext")
|
||||||
|
|
||||||
|
// subdirectories in a given directory
|
||||||
|
var flagvalTheme string
|
||||||
|
c.Flags().StringVar(&flagvalTheme, "theme", "", "theme to use (located in /themes/THEMENAME/)")
|
||||||
|
c.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})
|
||||||
|
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
c.GenBashCompletion(out)
|
c.GenBashCompletion(out)
|
||||||
str := out.String()
|
str := out.String()
|
||||||
|
@ -75,6 +80,8 @@ func TestBashCompletions(t *testing.T) {
|
||||||
check(t, str, `flags_completion+=("_filedir")`)
|
check(t, str, `flags_completion+=("_filedir")`)
|
||||||
// check for filename extension flags
|
// check for filename extension flags
|
||||||
check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)
|
check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)
|
||||||
|
// check for subdirs_in_dir flags
|
||||||
|
check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)
|
||||||
|
|
||||||
checkOmit(t, str, cmdDeprecated.Name())
|
checkOmit(t, str, cmdDeprecated.Name())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue