From 1a618fb24b01a0f393ccd82d55c4d8058bdeaf5c Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Wed, 21 Feb 2018 07:50:56 -0800 Subject: [PATCH 1/2] Do not add a space after a single flag completion (#625) --- bash_completions.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bash_completions.go b/bash_completions.go index 1d955ac2..897ed2cd 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -136,6 +136,12 @@ __%[1]s_handle_reply() if declare -F __ltrim_colon_completions >/dev/null; then __ltrim_colon_completions "$cur" 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" From a1e4933ab784095895e33dbe9f001ba10cfe2060 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Wed, 21 Feb 2018 09:51:53 -0800 Subject: [PATCH 2/2] 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 +```