diff --git a/bash_completions.go b/bash_completions.go index 8fa8f486..a11204da 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -268,7 +268,7 @@ __%[1]s_handle_word() `, name)) } -func writePostscript(buf *bytes.Buffer, name string) { +func writePostscript(buf *bytes.Buffer, name string, disableDefaultValues bool) { name = strings.Replace(name, ":", "__", -1) buf.WriteString(fmt.Sprintf("__start_%s()\n", name)) buf.WriteString(fmt.Sprintf(`{ @@ -297,13 +297,19 @@ func writePostscript(buf *bytes.Buffer, name string) { } `, name)) + + defaultsValue := "-o default" + if disableDefaultValues == true { + defaultsValue = "" + } + buf.WriteString(fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then - complete -o default -F __start_%s %s + complete %s -F __start_%s %s else - complete -o default -o nospace -F __start_%s %s + complete %s -o nospace -F __start_%s %s fi -`, name, name, name, name)) +`, defaultsValue, name, name, defaultsValue, name, name)) 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") } gen(buf, c) - writePostscript(buf, c.Name()) + writePostscript(buf, c.Name(), c.DisableBashCompletionDefaultValues) _, err := buf.WriteTo(w) return err diff --git a/bash_completions_test.go b/bash_completions_test.go index 02a4f15b..b025652f 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -64,7 +64,7 @@ func TestBashCompletions(t *testing.T) { ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"}, ValidArgs: []string{"pod", "node", "service", "replicationcontroller"}, BashCompletionFunction: bashCompletionFunc, - Run: emptyRun, + Run: emptyRun, } rootCmd.Flags().IntP("introot", "i", -1, "help message for flag 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) } } + +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) + } +} diff --git a/command.go b/command.go index 34d1bf36..9cf7f31e 100644 --- a/command.go +++ b/command.go @@ -133,6 +133,12 @@ type Command struct { // DisableSuggestions disables the suggestions based on Levenshtein distance // that go along with 'unknown command' messages. 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. // Must be > 0. SuggestionsMinimumDistance int