Merge pull request #141 from eparis/mac-completions

Enable bash completions on Macs
This commit is contained in:
Eric Paris 2015-09-04 09:11:03 -05:00
commit 68f5a81a72
1 changed files with 14 additions and 3 deletions

View File

@ -19,7 +19,6 @@ const (
func preamble(out *bytes.Buffer) { func preamble(out *bytes.Buffer) {
fmt.Fprintf(out, `#!/bin/bash fmt.Fprintf(out, `#!/bin/bash
__debug() __debug()
{ {
if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
@ -27,6 +26,14 @@ __debug()
fi fi
} }
# Homebrew on Macs have version 1.3 of bash-completion which doesn't include
# _init_completion. This is a very minimal version of that function.
__my_init_completion()
{
COMPREPLY=()
_get_comp_words_by_ref cur prev words cword
}
__index_of_word() __index_of_word()
{ {
local w word=$1 local w word=$1
@ -188,7 +195,11 @@ func postscript(out *bytes.Buffer, name string) {
fmt.Fprintf(out, "__start_%s()\n", name) fmt.Fprintf(out, "__start_%s()\n", name)
fmt.Fprintf(out, `{ fmt.Fprintf(out, `{
local cur prev words cword local cur prev words cword
if declare -F _init_completions >/dev/null 2>&1; then
_init_completion -s || return _init_completion -s || return
else
__my_init_completion || return
fi
local c=0 local c=0
local flags=() local flags=()
@ -292,7 +303,7 @@ func writeRequiredFlag(cmd *Command, out *bytes.Buffer) {
fmt.Fprintf(out, " must_have_one_flag=()\n") fmt.Fprintf(out, " must_have_one_flag=()\n")
flags := cmd.NonInheritedFlags() flags := cmd.NonInheritedFlags()
flags.VisitAll(func(flag *pflag.Flag) { flags.VisitAll(func(flag *pflag.Flag) {
for key, _ := range flag.Annotations { for key := range flag.Annotations {
switch key { switch key {
case BashCompOneRequiredFlag: case BashCompOneRequiredFlag:
format := " must_have_one_flag+=(\"--%s" format := " must_have_one_flag+=(\"--%s"