Skip to content

Commit 7ee208b

Browse files
rajatjindaleparis
authored andcommitted
support completions for command aliases (#669)
* support completions for command aliases * try newer version of shellcheck * initialize aliashash only when BASH_VERSION > 3
1 parent 6154259 commit 7ee208b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ base: &base
1313
name: "All Commands"
1414
command: |
1515
mkdir -p bin
16-
curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.3/shellcheck
16+
curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.6/shellcheck
1717
chmod +x bin/shellcheck
1818
go get -t -v ./...
1919
PATH=$PATH:$PWD/bin go test -v ./...

bash_completions.go

+29
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ __%[1]s_handle_word()
251251
__%[1]s_handle_command
252252
elif [[ $c -eq 0 ]]; then
253253
__%[1]s_handle_command
254+
elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then
255+
# aliashash variable is an associative array which is only supported in bash > 3.
256+
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
257+
words[c]=${aliashash[${words[c]}]}
258+
__%[1]s_handle_command
259+
else
260+
__%[1]s_handle_noun
261+
fi
254262
else
255263
__%[1]s_handle_noun
256264
fi
@@ -266,6 +274,7 @@ func writePostscript(buf *bytes.Buffer, name string) {
266274
buf.WriteString(fmt.Sprintf(`{
267275
local cur prev words cword
268276
declare -A flaghash 2>/dev/null || :
277+
declare -A aliashash 2>/dev/null || :
269278
if declare -F _init_completion >/dev/null 2>&1; then
270279
_init_completion -s || return
271280
else
@@ -305,6 +314,7 @@ func writeCommands(buf *bytes.Buffer, cmd *Command) {
305314
continue
306315
}
307316
buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))
317+
writeCmdAliases(buf, c)
308318
}
309319
buf.WriteString("\n")
310320
}
@@ -443,6 +453,21 @@ func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
443453
}
444454
}
445455

456+
func writeCmdAliases(buf *bytes.Buffer, cmd *Command) {
457+
if len(cmd.Aliases) == 0 {
458+
return
459+
}
460+
461+
sort.Sort(sort.StringSlice(cmd.Aliases))
462+
463+
buf.WriteString(fmt.Sprint(` if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))
464+
for _, value := range cmd.Aliases {
465+
buf.WriteString(fmt.Sprintf(" command_aliases+=(%q)\n", value))
466+
buf.WriteString(fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))
467+
}
468+
buf.WriteString(` fi`)
469+
buf.WriteString("\n")
470+
}
446471
func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
447472
buf.WriteString(" noun_aliases=()\n")
448473
sort.Sort(sort.StringSlice(cmd.ArgAliases))
@@ -469,6 +494,10 @@ func gen(buf *bytes.Buffer, cmd *Command) {
469494
}
470495

471496
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
497+
buf.WriteString("\n")
498+
buf.WriteString(" command_aliases=()\n")
499+
buf.WriteString("\n")
500+
472501
writeCommands(buf, cmd)
473502
writeFlags(buf, cmd)
474503
writeRequiredFlag(buf, cmd)

0 commit comments

Comments
 (0)