From a1e4933ab784095895e33dbe9f001ba10cfe2060 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Wed, 21 Feb 2018 09:51:53 -0800 Subject: [PATCH] Bash completion aliases (#638) * alias support with bash completions * add cmdname to rootcmdname * remove print statement * add documentation for bash alias --- bash_completions.go | 12 +++++++++--- bash_completions.md | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 897ed2cd..d898630b 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -228,7 +228,7 @@ __%[1]s_handle_command() next_command="_${last_command}_${words[c]//:/__}" else if [[ $c -eq 0 ]]; then - next_command="_$(basename "${words[c]//:/__}")" + next_command="_%[1]s_root_command" else next_command="_${words[c]//:/__}" fi @@ -249,7 +249,7 @@ __%[1]s_handle_word() __%[1]s_handle_flag elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then __%[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 else __%[1]s_handle_noun @@ -461,7 +461,13 @@ func gen(buf *bytes.Buffer, cmd *Command) { commandName := cmd.CommandPath() commandName = strings.Replace(commandName, " ", "_", -1) commandName = strings.Replace(commandName, ":", "__", -1) - buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName)) + + 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(" last_command=%q\n", commandName)) writeCommands(buf, cmd) writeFlags(buf, cmd) diff --git a/bash_completions.md b/bash_completions.md index 44d777c1..691a04e1 100644 --- a/bash_completions.md +++ b/bash_completions.md @@ -204,3 +204,17 @@ __kubectl_get_namespaces() 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 +completion firstcommand secondcommand +```