Zend certified PHP/Magento developer

Add shell (bash/zsh) tab completion to custom command by running existing completion with arguments

In our code base we have a few convenience executables that are automatically loaded to the environment for anyone cloning the code (using direnv).

This is a general question, but for the purpose of this question let’s use kubectl as an example.

We have this executable file named kl:

#!/usr/bin/env bash  

STACK="$1"
exec kubectl --context=our_cluster_name --namespace="$STACK" "$@"

Then we have variations for the prod, staging and dev namespaces (klp, kls and kld respectively) e.g. kls:

#!/usr/bin/env bash

exec kl staging "$@"

kubectl has fantastic auto-completion, but if we use any of the kl variations, we don’t get auto complete. I’ve done a lot of searches here and couldn’t find how I can utilize an existing completion function to implement new completions.

I ran:

$ complete | grep kubectl

and got

complete -o default -F __start_kubectl kubectl
complete -F _minimal __start_kubectl

Basically what I want is a new entry there so that kl[d|s|p] will be completed with __start_kubectl but as if it already has the --context and --namespace arguments populated.

Ideally we’d want a solution that works for both Bash and Zsh.

Thanks!