mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Bash completion aliases (#638)
* alias support with bash completions * add cmdname to rootcmdname * remove print statement * add documentation for bash alias
This commit is contained in:
parent
1a618fb24b
commit
a1e4933ab7
2 changed files with 23 additions and 3 deletions
|
@ -228,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
|
||||||
|
@ -249,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
|
||||||
|
@ -461,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…
Reference in a new issue