Merge pull request #54406 from superbrothers/kubectl-override-short-flags

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Improve 'kubectl completion` to override short flags

**What this PR does / why we need it**: This PR improves `kubectl completion` to override short flags in
addition to long flags. I tested this PR in both bash and zsh.

```
# Complete pods name in the `default` namespace
$ kubectl get po nginx-4217019353-<tab>
nginx-4217019353-mw1pk  nginx-4217019353-rzw2c

# Complete pods name in the `kube-system` namepace due to overriding the namespace
$ kubectl -n kube-system get po kube<tab>
kube-addon-manager-minikube  kube-dns-910330662-l9pwt     kubernetes-dashboard-9fbhm
```

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-10-31 22:32:54 -07:00 committed by GitHub
commit 7f9f847ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,22 +37,22 @@ import (
const (
bashCompletionFunc = `# call kubectl get $1,
__kubectl_override_flag_list=(kubeconfig cluster user context namespace server)
__kubectl_override_flag_list=(--kubeconfig --cluster --user --context --namespace --server -n -s)
__kubectl_override_flags()
{
local ${__kubectl_override_flag_list[*]} two_word_of of
local ${__kubectl_override_flag_list[*]##*-} two_word_of of var
for w in "${words[@]}"; do
if [ -n "${two_word_of}" ]; then
eval "${two_word_of}=\"--${two_word_of}=\${w}\""
eval "${two_word_of##*-}=\"${two_word_of}=\${w}\""
two_word_of=
continue
fi
for of in "${__kubectl_override_flag_list[@]}"; do
case "${w}" in
--${of}=*)
eval "${of}=\"${w}\""
${of}=*)
eval "${of##*-}=\"${w}\""
;;
--${of})
${of})
two_word_of="${of}"
;;
esac
@ -61,9 +61,9 @@ __kubectl_override_flags()
namespace="--all-namespaces"
fi
done
for of in "${__kubectl_override_flag_list[@]}"; do
if eval "test -n \"\$${of}\""; then
eval "echo \${${of}}"
for var in "${__kubectl_override_flag_list[@]##*-}"; do
if eval "test -n \"\$${var}\""; then
eval "echo \${${var}}"
fi
done
}