Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable builtin completion command for root command with only flags #1954

Closed
dbkegley opened this issue Apr 27, 2023 · 2 comments
Closed

Enable builtin completion command for root command with only flags #1954

dbkegley opened this issue Apr 27, 2023 · 2 comments

Comments

@dbkegley
Copy link

I'm working on adding shell completion for my kubectl plugin now that kubernetes/kubernetes#105867 is merged (thank you so much for this btw. I wouldn't even be attempting this otherwise).

My plugin does not have any sub-commands, it only provides flags currently. I was surprised to find that I have to add a hidden sub-command in order to expose the completion builtin command.

I was able to make this work in the end, but it feels awkward to do it this way, so I thought I should file an issue in case this wasn't the intended behavior:
https://github.com/B23admin/kubectl-config-cleanup/blob/feeb2f1d52c8304a2bf4aa3f4af1dc18bfce4154/cleanup/cleanup.go#L106-L114

I did come across this comment to someone who was having a similar problem, however I'm calling Execute() so I think my situation is a little different

@marckhouzam
Copy link
Collaborator

I believe you need #1559 (which I kind of forgot about).

Your workaround of adding an empty hidden subcommand is imaginative, I hadn't considered it. However IIRC it will have some undesirable impacts on how your program processes arguments. This is why we couldn't go with #1450. But maybe it's safe in you specific case?

But let me first ask why you need the completion command? If you run your plugin through kubectl, that command is not needed. But I agree that if you want to also run your plugin as a standalone binary then that command is useful. Just curious what your use case might be?

I'm working on adding shell completion for my kubectl plugin now that kubernetes/kubernetes#105867 is merged (thank you so much for this btw. I wouldn't even be attempting this otherwise).

That's nice to read. Thank you.

@dbkegley
Copy link
Author

dbkegley commented Apr 28, 2023

But let me first ask why you need the completion command?

Oh! I didn't realize that __complete was available without the completion command. I've just tested this again and it's working as advertised. I've installed the following completion script on my path as described in your original PR:

#!/usr/bin/env sh
kubectl config-cleanup __complete "$@"

The only thing that isn't quite right is the completion suggestions. The -h/--help flag seems to be duplicated for some reason. Any idea why that might be happening?

kubectl config-cleanup -[TAB]
--clusters                       -- Cleanup cluster entries which are not specified by a context
--help           --help  -h  -h  -- help for config-cleanup
--kubeconfig                     -- Specify a kubeconfig file to cleanup
--print-removed                  -- Print the removed contents of the kubeconfig after cleanup
--raw                            -- Print the raw contents of the kubeconfig after cleanup, suitable for piping to a new file
--timeout        -t              -- Seconds to wait for a response from the server before continuing cleanup
--users                          -- Cleanup user entries which are not specified by a context
--version        -v              -- version for config-cleanup

The -h/--help flags are not duplicated by the __complete command however.

kubectl config-cleanup __complete -
--clusters	Cleanup cluster entries which are not specified by a context
--help	help for config-cleanup
-h	help for config-cleanup
--kubeconfig	Specify a kubeconfig file to cleanup
--print-removed	Print the removed contents of the kubeconfig after cleanup
--raw	Print the raw contents of the kubeconfig after cleanup, suitable for piping to a new file
--timeout	Seconds to wait for a response from the server before continuing cleanup
-t	Seconds to wait for a response from the server before continuing cleanup
--users	Cleanup user entries which are not specified by a context
--version	version for config-cleanup
-v	version for config-cleanup
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

#1559 would be nice to have, but given that the __complete command is always available, I don't think I need it for what I'm trying to accomplish here. I'll go ahead and close this issue. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants