-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨ feat(CLI): Add completion for the plugins flag #5596
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,6 +103,7 @@ func (c CLI) newRootCmd() *cobra.Command { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Global flags for all subcommands. | ||||||||||||||||||||||
| cmd.PersistentFlags().StringSlice(pluginsFlag, nil, "plugin keys to be used for this subcommand execution") | ||||||||||||||||||||||
| cobra.CheckErr(cmd.RegisterFlagCompletionFunc(pluginsFlag, c.completionPluginsFlag)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Register --project-version on the root command so that it shows up in help. | ||||||||||||||||||||||
| cmd.Flags().String(projectVersionFlag, c.defaultProjectVersion.String(), "project version") | ||||||||||||||||||||||
|
|
@@ -252,3 +253,48 @@ func (c CLI) getPluginTableFilteredWithOptions(filter func(plugin.Plugin) bool, | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| return strings.Join(lines, "\n") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // completionPluginsFlag implements cobra.CompletionFunc and is registered | ||||||||||||||||||||||
| // as the flag completion function for --plugins | ||||||||||||||||||||||
| // We should note that flag completion does not work for comma-chained values | ||||||||||||||||||||||
| // but works fine when repeating the flag | ||||||||||||||||||||||
| func (c CLI) completionPluginsFlag( | ||||||||||||||||||||||
| cmd *cobra.Command, | ||||||||||||||||||||||
| _ []string, | ||||||||||||||||||||||
| _ string, | ||||||||||||||||||||||
| ) ([]string, cobra.ShellCompDirective) { | ||||||||||||||||||||||
| // We filter strings that the user already passed to --plugins, | ||||||||||||||||||||||
| // in case the user chains the --plugins flag multiple times, | ||||||||||||||||||||||
| alreadyEntered, err := cmd.Flags().GetStringSlice(pluginsFlag) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| cobra.CheckErr(err) | ||||||||||||||||||||||
|
||||||||||||||||||||||
| cobra.CheckErr(err) | |
| return nil, cobra.ShellCompDirectiveError |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The completion list is built by ranging over a map, so the suggestion order will be non-deterministic between runs. For a stable UX (and deterministic tests if added later), collect keys and sort them before appending to comps.
| for pluginKey, p := range c.plugins { | |
| pluginKeys := make([]string, 0, len(c.plugins)) | |
| for pluginKey := range c.plugins { | |
| pluginKeys = append(pluginKeys, pluginKey) | |
| } | |
| slices.Sort(pluginKeys) | |
| for _, pluginKey := range pluginKeys { | |
| p := c.plugins[pluginKey] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newRootCmd() already defines --plugins as a PersistentFlag; re-declaring the same flag via c.cmd.Flags().StringSlice risks a duplicate-flag panic and also means the test may not be exercising the real persistent-flag behavior. Remove the extra StringSlice() call and just Set() the existing flag (or set it on PersistentFlags()).