Add flag that gives the ability to disable bash default completion

This commit is contained in:
Georgios Andrianakis 2018-10-04 17:57:23 +03:00
parent a114f312e0
commit 08b4554a09
3 changed files with 44 additions and 6 deletions

View file

@ -268,7 +268,7 @@ __%[1]s_handle_word()
`, name)) `, name))
} }
func writePostscript(buf *bytes.Buffer, name string) { func writePostscript(buf *bytes.Buffer, name string, disableDefaultValues bool) {
name = strings.Replace(name, ":", "__", -1) name = strings.Replace(name, ":", "__", -1)
buf.WriteString(fmt.Sprintf("__start_%s()\n", name)) buf.WriteString(fmt.Sprintf("__start_%s()\n", name))
buf.WriteString(fmt.Sprintf(`{ buf.WriteString(fmt.Sprintf(`{
@ -297,13 +297,19 @@ func writePostscript(buf *bytes.Buffer, name string) {
} }
`, name)) `, name))
defaultsValue := "-o default"
if disableDefaultValues == true {
defaultsValue = ""
}
buf.WriteString(fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then buf.WriteString(fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_%s %s complete %s -F __start_%s %s
else else
complete -o default -o nospace -F __start_%s %s complete %s -o nospace -F __start_%s %s
fi fi
`, name, name, name, name)) `, defaultsValue, name, name, defaultsValue, name, name))
buf.WriteString("# ex: ts=4 sw=4 et filetype=sh\n") buf.WriteString("# ex: ts=4 sw=4 et filetype=sh\n")
} }
@ -514,7 +520,7 @@ func (c *Command) GenBashCompletion(w io.Writer) error {
buf.WriteString(c.BashCompletionFunction + "\n") buf.WriteString(c.BashCompletionFunction + "\n")
} }
gen(buf, c) gen(buf, c)
writePostscript(buf, c.Name()) writePostscript(buf, c.Name(), c.DisableBashCompletionDefaultValues)
_, err := buf.WriteTo(w) _, err := buf.WriteTo(w)
return err return err

View file

@ -64,7 +64,7 @@ func TestBashCompletions(t *testing.T) {
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"}, ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"}, ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
BashCompletionFunction: bashCompletionFunc, BashCompletionFunction: bashCompletionFunc,
Run: emptyRun, Run: emptyRun,
} }
rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot") rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")
rootCmd.MarkFlagRequired("introot") rootCmd.MarkFlagRequired("introot")
@ -215,3 +215,29 @@ func TestBashCompletionDeprecatedFlag(t *testing.T) {
t.Errorf("expected completion to not include %q flag: Got %v", flagName, output) t.Errorf("expected completion to not include %q flag: Got %v", flagName, output)
} }
} }
func TestBashCompletionDisableDefaultValuesSet(t *testing.T) {
c := &Command{Run: emptyRun, DisableBashCompletionDefaultValues: true}
buf := new(bytes.Buffer)
c.GenBashCompletion(buf)
output := buf.String()
const defaultOutput = "-o default"
if strings.Contains(output, defaultOutput) {
t.Errorf("expected completion to not include %q flag: Got %v", defaultOutput, output)
}
}
func TestBashCompletionDisableDefaultValuesUnset(t *testing.T) {
c := &Command{Run: emptyRun}
buf := new(bytes.Buffer)
c.GenBashCompletion(buf)
output := buf.String()
const defaultOutput = "-o default"
if !strings.Contains(output, defaultOutput) {
t.Errorf("expected completion to include %q flag: Got %v", defaultOutput, output)
}
}

View file

@ -133,6 +133,12 @@ type Command struct {
// DisableSuggestions disables the suggestions based on Levenshtein distance // DisableSuggestions disables the suggestions based on Levenshtein distance
// that go along with 'unknown command' messages. // that go along with 'unknown command' messages.
DisableSuggestions bool DisableSuggestions bool
// DisableBashCompletionDefaultValues disables the default values that bash automatically
// suggests when the functions generated by the library ultimately don't returns any results
// The default suggestions bash gives are the files in the current directory
DisableBashCompletionDefaultValues bool
// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
// Must be > 0. // Must be > 0.
SuggestionsMinimumDistance int SuggestionsMinimumDistance int