@@ -251,6 +251,14 @@ __%[1]s_handle_word()
251
251
__%[1]s_handle_command
252
252
elif [[ $c -eq 0 ]]; then
253
253
__%[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
254
262
else
255
263
__%[1]s_handle_noun
256
264
fi
@@ -266,6 +274,7 @@ func writePostscript(buf *bytes.Buffer, name string) {
266
274
buf .WriteString (fmt .Sprintf (`{
267
275
local cur prev words cword
268
276
declare -A flaghash 2>/dev/null || :
277
+ declare -A aliashash 2>/dev/null || :
269
278
if declare -F _init_completion >/dev/null 2>&1; then
270
279
_init_completion -s || return
271
280
else
@@ -305,6 +314,7 @@ func writeCommands(buf *bytes.Buffer, cmd *Command) {
305
314
continue
306
315
}
307
316
buf .WriteString (fmt .Sprintf (" commands+=(%q)\n " , c .Name ()))
317
+ writeCmdAliases (buf , c )
308
318
}
309
319
buf .WriteString ("\n " )
310
320
}
@@ -443,6 +453,21 @@ func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
443
453
}
444
454
}
445
455
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
+ }
446
471
func writeArgAliases (buf * bytes.Buffer , cmd * Command ) {
447
472
buf .WriteString (" noun_aliases=()\n " )
448
473
sort .Sort (sort .StringSlice (cmd .ArgAliases ))
@@ -469,6 +494,10 @@ func gen(buf *bytes.Buffer, cmd *Command) {
469
494
}
470
495
471
496
buf .WriteString (fmt .Sprintf (" last_command=%q\n " , commandName ))
497
+ buf .WriteString ("\n " )
498
+ buf .WriteString (" command_aliases=()\n " )
499
+ buf .WriteString ("\n " )
500
+
472
501
writeCommands (buf , cmd )
473
502
writeFlags (buf , cmd )
474
503
writeRequiredFlag (buf , cmd )
0 commit comments