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

DisableFlagParsing not respected by completion logic #1160

Closed
marckhouzam opened this issue Jul 11, 2020 · 3 comments · Fixed by #1161
Closed

DisableFlagParsing not respected by completion logic #1160

marckhouzam opened this issue Jul 11, 2020 · 3 comments · Fixed by #1161

Comments

@marckhouzam
Copy link
Collaborator

When a command sets DisableFlagParsing=true it requests that Cobra not handle flags but instead pass them directly to the Run functions to let the command handle them itself. However, the completion logic does not do the same. Instead, it attempts to complete flag names even if DisableFlagParsing==true.

The completion logic should instead ignore flags when DisableFlagParsing==true and pass them directly to ValidArgsFunction to let the command handle them itself.

Here is the usecase. helm supports plugins which are loaded at runtime. A plugin supports its own flags of which helm (and consequently Cobra) is not aware. helm registers a command with Cobra to represent the plugin and sets DisableFlagParsing=true to let the plugin itself handle its flags. helm also defines a ValidArgsFunction which calls the plugin in question to perform completion. The problem is that when completing flag names, Cobra attempts to do the completion itself instead of calling ValidArgsFunction, so the plugin is not asked to provide completion for its flags.

Here is a concrete example of the problem.
If we define the following command:

var plugin = &cobra.Command{
	Use:   "plugin",
	Short: "plugin with disable flags",
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		cobra.CompDebugln(fmt.Sprintf("ValidArgsFunction called with args %v and toComplete %s\n", args, toComplete), true)
		return nil, cobra.ShellCompDirectiveDefault
	},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("Run function called with args: %v\n", args)
	},
	DisableFlagParsing: true,
}

then the Run command is properly called:

$ ./testprog plugin --randomflag
Run function called with args: [--randomflag]

but the ValidArgsFunction is not:

$ ./testprog __complete plugin --randomfl
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
# We should have seen the debug printout saying ValidArgsFunction was called
@marckhouzam
Copy link
Collaborator Author

I'll be posting a PR to address this.

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator Author

PR posted at #1161

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

Successfully merging a pull request may close this issue.

1 participant