forked from mirror/cobra
support completions for command aliases (#669)
* support completions for command aliases * try newer version of shellcheck * initialize aliashash only when BASH_VERSION > 3
This commit is contained in:
parent
615425954c
commit
7ee208b09f
|
@ -13,7 +13,7 @@ base: &base
|
||||||
name: "All Commands"
|
name: "All Commands"
|
||||||
command: |
|
command: |
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.3/shellcheck
|
curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.6/shellcheck
|
||||||
chmod +x bin/shellcheck
|
chmod +x bin/shellcheck
|
||||||
go get -t -v ./...
|
go get -t -v ./...
|
||||||
PATH=$PATH:$PWD/bin go test -v ./...
|
PATH=$PATH:$PWD/bin go test -v ./...
|
||||||
|
|
|
@ -251,6 +251,14 @@ __%[1]s_handle_word()
|
||||||
__%[1]s_handle_command
|
__%[1]s_handle_command
|
||||||
elif [[ $c -eq 0 ]]; then
|
elif [[ $c -eq 0 ]]; then
|
||||||
__%[1]s_handle_command
|
__%[1]s_handle_command
|
||||||
|
elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then
|
||||||
|
# aliashash variable is an associative array which is only supported in bash > 3.
|
||||||
|
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
|
||||||
|
words[c]=${aliashash[${words[c]}]}
|
||||||
|
__%[1]s_handle_command
|
||||||
|
else
|
||||||
|
__%[1]s_handle_noun
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
__%[1]s_handle_noun
|
__%[1]s_handle_noun
|
||||||
fi
|
fi
|
||||||
|
@ -266,6 +274,7 @@ func writePostscript(buf *bytes.Buffer, name string) {
|
||||||
buf.WriteString(fmt.Sprintf(`{
|
buf.WriteString(fmt.Sprintf(`{
|
||||||
local cur prev words cword
|
local cur prev words cword
|
||||||
declare -A flaghash 2>/dev/null || :
|
declare -A flaghash 2>/dev/null || :
|
||||||
|
declare -A aliashash 2>/dev/null || :
|
||||||
if declare -F _init_completion >/dev/null 2>&1; then
|
if declare -F _init_completion >/dev/null 2>&1; then
|
||||||
_init_completion -s || return
|
_init_completion -s || return
|
||||||
else
|
else
|
||||||
|
@ -305,6 +314,7 @@ func writeCommands(buf *bytes.Buffer, cmd *Command) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))
|
buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))
|
||||||
|
writeCmdAliases(buf, c)
|
||||||
}
|
}
|
||||||
buf.WriteString("\n")
|
buf.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
@ -443,6 +453,21 @@ func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeCmdAliases(buf *bytes.Buffer, cmd *Command) {
|
||||||
|
if len(cmd.Aliases) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(sort.StringSlice(cmd.Aliases))
|
||||||
|
|
||||||
|
buf.WriteString(fmt.Sprint(` if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))
|
||||||
|
for _, value := range cmd.Aliases {
|
||||||
|
buf.WriteString(fmt.Sprintf(" command_aliases+=(%q)\n", value))
|
||||||
|
buf.WriteString(fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))
|
||||||
|
}
|
||||||
|
buf.WriteString(` fi`)
|
||||||
|
buf.WriteString("\n")
|
||||||
|
}
|
||||||
func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
|
func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
|
||||||
buf.WriteString(" noun_aliases=()\n")
|
buf.WriteString(" noun_aliases=()\n")
|
||||||
sort.Sort(sort.StringSlice(cmd.ArgAliases))
|
sort.Sort(sort.StringSlice(cmd.ArgAliases))
|
||||||
|
@ -469,6 +494,10 @@ func gen(buf *bytes.Buffer, cmd *Command) {
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
|
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
|
||||||
|
buf.WriteString("\n")
|
||||||
|
buf.WriteString(" command_aliases=()\n")
|
||||||
|
buf.WriteString("\n")
|
||||||
|
|
||||||
writeCommands(buf, cmd)
|
writeCommands(buf, cmd)
|
||||||
writeFlags(buf, cmd)
|
writeFlags(buf, cmd)
|
||||||
writeRequiredFlag(buf, cmd)
|
writeRequiredFlag(buf, cmd)
|
||||||
|
|
Loading…
Reference in New Issue