Skip to content

Commit

Permalink
Bash completion aliases (#638)
Browse files Browse the repository at this point in the history
* alias support with bash completions

* add cmdname to rootcmdname

* remove print statement

* add documentation for bash alias
  • Loading branch information
rajatjindal authored and eparis committed Feb 21, 2018
1 parent 1a618fb commit a1e4933
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bash_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions bash_completions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tab><tab>
completion firstcommand secondcommand
```

0 comments on commit a1e4933

Please sign in to comment.