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
Skip flag completion for commands with disabled flag parsing #1456
Conversation
When a command disabled flag parsing, the shell completion for that command should not offer flags. This allows completions for args to be processed like normal. This behavior is particularly useful for invoking a CLI plugin that has its own sub commands, arguments, flags, and completion rules. Signed-off-by: Scott Andrews <andrewssc@vmware.com>
cc @marckhouzam - would be great to get your opinion on this |
Thanks @scothis. There is already some special handling for the DisableFlagParsing option in the completion code but it may not cover every case. Could you provide an example scenario to better illustrate the case you refer to? |
The basic problem I was hitting is that cobra either wants to provide completions for commands/args or completions for flags. In a CLI I've been contributing to (sorry it's not public) has a plugin model where plugins are mounted into the root command as child commands that then exec the plugin passing the remaining args. These commands are using This PR is attempting to make cobra completion also ignore flags when |
I'm glad someone else needs this fix @scothis. This is the same problem as what #1161 is trying to fix. However, after experimenting with the fix, #1161 chooses to still do flag completion when
still performs the completion for Would that be a scenario valuable for you as well? Note that #1161 seems to have a big diff, but it is mostly just indentation changes, the actual code changes are quite small. |
@marckhouzam good to see someone else thinking along similar lines. I wonder if cobra should take any responsibility for persistent flags when the command has asked for flag parsing to be disabled. This is something that could be addressed in user space by the custom completion for the command taking flags into consideration. As a meta question, I'm curious as to why cobra draws such a strong distinction between completions for commands/args and flags. |
My thought was that it simplifies the responsibility of the plugin developer. Also, if we don't do that and let plugins deal with completion of global flag names, then, when a new global flag is added, every plugin will have to be updated. Do you worry about a scenario in particular?
It doesn't really, it's just that completion of args and commands always trigger |
Another way to solve the issue would be to allow flags and args to complete simultaniously. The root cause of the issue that drove me to open this PR was that when flag completion triggered, there was no opportunity to provide custom arg completion. Something that could be a bit sticky is handling conflicting ShellCompDirectives.
I assume you'd add this behavior once in the command that is hosting the plugin, and not in every plugin. When adding a new persistent flag, don't you already have the issue of how to pass that to the plugin? Our plugin model doesn't have any persistent flags from the root command, so we've side stepped this issue so far. |
IIUC, this would imply that when Cobra does flag name completion, it would also call Also, I think changing this behaviour now would cause some backwards-compatibility issues for users of Cobra.
You are probably right about that. There is another scenario where flag-name completion is required for the Helm plugin system (which I tried to avoid getting into, as it is harder to explain). Basically helm allows a plugin to provide a In this solution, Cobra knows about flag-names, but the plugin sub-command still sets Did you try using #1161 with your tool to confirm that it would satisfy your needs? |
@marckhouzam thanks for the reminder. #1161 does also solve our issue. I only tested with the |
Let's stick with #1161 since it will provide a bit more flexibility. Thanks for the great conversation on this one everyone! |
When a command disabled flag parsing, the shell completion for that
command should not offer flags. This allows completions for args to be
processed like normal. This behavior is particularly useful for invoking
a CLI plugin that has its own sub commands, arguments, flags, and
completion rules.
Signed-off-by: Scott Andrews andrewssc@vmware.com