diff --git a/bash_completions.go b/bash_completions.go index c19fe7a0..2e14d68b 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -219,7 +219,7 @@ __handle_command() next_command="_${last_command}_${words[c]//:/__}" else if [[ $c -eq 0 ]]; then - next_command="_$(basename "${words[c]//:/__}")" + next_command="_root_command" else next_command="_${words[c]//:/__}" fi @@ -240,7 +240,7 @@ __handle_word() __handle_flag elif __contains_word "${words[c]}" "${commands[@]}"; then __handle_command - elif [[ $c -eq 0 ]] && __contains_word "$(basename "${words[c]}")" "${commands[@]}"; then + elif [[ $c -eq 0 ]]; then __handle_command else __handle_noun @@ -452,7 +452,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.Sprint("_root_command()\n{\n")) + } else { + buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName)) + } + buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName)) writeCommands(buf, cmd) writeFlags(buf, cmd)