mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
support completions for command aliases
This commit is contained in:
parent
cd30c2a7e9
commit
947f0efbe0
1 changed files with 22 additions and 0 deletions
|
@ -251,6 +251,14 @@ __%[1]s_handle_word()
|
|||
__%[1]s_handle_command
|
||||
elif [[ $c -eq 0 ]]; then
|
||||
__%[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
|
||||
__%[1]s_handle_noun
|
||||
fi
|
||||
|
@ -266,6 +274,7 @@ func writePostscript(buf *bytes.Buffer, name string) {
|
|||
buf.WriteString(fmt.Sprintf(`{
|
||||
local cur prev words cword
|
||||
declare -A flaghash 2>/dev/null || :
|
||||
declare -A aliashash 2>/dev/null || :
|
||||
if declare -F _init_completion >/dev/null 2>&1; then
|
||||
_init_completion -s || return
|
||||
else
|
||||
|
@ -305,6 +314,7 @@ func writeCommands(buf *bytes.Buffer, cmd *Command) {
|
|||
continue
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))
|
||||
writeCmdAliases(buf, c)
|
||||
}
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
|
@ -443,6 +453,14 @@ func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
|
|||
}
|
||||
}
|
||||
|
||||
func writeCmdAliases(buf *bytes.Buffer, cmd *Command) {
|
||||
sort.Sort(sort.StringSlice(cmd.Aliases))
|
||||
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("\n")
|
||||
}
|
||||
func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
|
||||
buf.WriteString(" noun_aliases=()\n")
|
||||
sort.Sort(sort.StringSlice(cmd.ArgAliases))
|
||||
|
@ -469,6 +487,10 @@ func gen(buf *bytes.Buffer, cmd *Command) {
|
|||
}
|
||||
|
||||
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
|
||||
buf.WriteString("\n")
|
||||
buf.WriteString(" command_aliases=()\n")
|
||||
buf.WriteString("\n")
|
||||
|
||||
writeCommands(buf, cmd)
|
||||
writeFlags(buf, cmd)
|
||||
writeRequiredFlag(buf, cmd)
|
||||
|
|
Loading…
Add table
Reference in a new issue