mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Merge remote-tracking branch 'spf13/master' into zsh-completion
This commit is contained in:
commit
c978c0bd7d
2 changed files with 29 additions and 3 deletions
|
@ -136,6 +136,12 @@ __%[1]s_handle_reply()
|
||||||
if declare -F __ltrim_colon_completions >/dev/null; then
|
if declare -F __ltrim_colon_completions >/dev/null; then
|
||||||
__ltrim_colon_completions "$cur"
|
__ltrim_colon_completions "$cur"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# If there is only 1 completion and it is a flag with an = it will be completed
|
||||||
|
# but we don't want a space after the =
|
||||||
|
if [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ $(type -t compopt) = "builtin" ]] && [[ "${COMPREPLY[0]}" == --*= ]]; then
|
||||||
|
compopt -o nospace
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# The arguments should be in the form "ext1|ext2|extn"
|
# The arguments should be in the form "ext1|ext2|extn"
|
||||||
|
@ -222,7 +228,7 @@ __%[1]s_handle_command()
|
||||||
next_command="_${last_command}_${words[c]//:/__}"
|
next_command="_${last_command}_${words[c]//:/__}"
|
||||||
else
|
else
|
||||||
if [[ $c -eq 0 ]]; then
|
if [[ $c -eq 0 ]]; then
|
||||||
next_command="_$(basename "${words[c]//:/__}")"
|
next_command="_%[1]s_root_command"
|
||||||
else
|
else
|
||||||
next_command="_${words[c]//:/__}"
|
next_command="_${words[c]//:/__}"
|
||||||
fi
|
fi
|
||||||
|
@ -243,7 +249,7 @@ __%[1]s_handle_word()
|
||||||
__%[1]s_handle_flag
|
__%[1]s_handle_flag
|
||||||
elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then
|
elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then
|
||||||
__%[1]s_handle_command
|
__%[1]s_handle_command
|
||||||
elif [[ $c -eq 0 ]] && __%[1]s_contains_word "$(basename "${words[c]}")" "${commands[@]}"; then
|
elif [[ $c -eq 0 ]]; then
|
||||||
__%[1]s_handle_command
|
__%[1]s_handle_command
|
||||||
else
|
else
|
||||||
__%[1]s_handle_noun
|
__%[1]s_handle_noun
|
||||||
|
@ -455,7 +461,13 @@ func gen(buf *bytes.Buffer, cmd *Command) {
|
||||||
commandName := cmd.CommandPath()
|
commandName := cmd.CommandPath()
|
||||||
commandName = strings.Replace(commandName, " ", "_", -1)
|
commandName = strings.Replace(commandName, " ", "_", -1)
|
||||||
commandName = strings.Replace(commandName, ":", "__", -1)
|
commandName = strings.Replace(commandName, ":", "__", -1)
|
||||||
|
|
||||||
|
if cmd.Root() == cmd {
|
||||||
|
buf.WriteString(fmt.Sprintf("_%s_root_command()\n{\n", commandName))
|
||||||
|
} else {
|
||||||
buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))
|
buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))
|
||||||
|
}
|
||||||
|
|
||||||
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
|
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
|
||||||
writeCommands(buf, cmd)
|
writeCommands(buf, cmd)
|
||||||
writeFlags(buf, cmd)
|
writeFlags(buf, cmd)
|
||||||
|
|
|
@ -204,3 +204,17 @@ __kubectl_get_namespaces()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
# Using bash aliases for commands
|
||||||
|
|
||||||
|
You can also configure the `bash aliases` for the commands and they will also support completions.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
alias aliasname=origcommand
|
||||||
|
complete -o default -F __start_origcommand aliasname
|
||||||
|
|
||||||
|
# and now when you run `aliasname` completion will make
|
||||||
|
# suggestions as it did for `origcommand`.
|
||||||
|
|
||||||
|
$) aliasname <tab><tab>
|
||||||
|
completion firstcommand secondcommand
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue