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

Completion helper #2222

Open
ccoVeille opened this issue Jan 22, 2025 · 3 comments
Open

Completion helper #2222

ccoVeille opened this issue Jan 22, 2025 · 3 comments

Comments

@ccoVeille
Copy link
Contributor

ccoVeille commented Jan 22, 2025

Following #2220

Right now the format for completion is not obvious. I discovered in ov tool

rootCmd.PersistentFlags().IntP("tab-width", "x", 8, "tab stop width")
_ = rootCmd.RegisterFlagCompletionFunc("tab-width", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	return []string{"3\ttab width", "2\ttab width", "4\ttab width", "8\ttab width"}, cobra.ShellCompDirectiveNoFileComp
})

https://github.com/noborus/ov/blob/4f4e9e5c266ec4815c5a166718e318992ea47751/main.go#L431-433

By not .obvious, I mean the tab is logic but not something you will think of, when you easily find out in the documentation.

I opened an issue as it requires some design

  • Adding a struct, with Text and description

  • validation might be needed

  • Generic could help, but the Go version 1.15 is go.mod broke my dreams 😅

  • a fmt.Stringer art, but it doesn't bring much

I might be overthinking and what is needed is simply about adding documentation about the format in the methods

So, let's talk 😁

@ccoVeille
Copy link
Contributor Author

ccoVeille commented Jan 22, 2025

Of course, we could start by adding Go Examples to these methods.

Then implements something with an helper

@marckhouzam
Copy link
Collaborator

There is a small mention of this in the completions README:

Cobra allows you to add descriptions to your own completions. Simply add the description text after each completion, following a `\t` separator. This technique applies to completions returned by `ValidArgs`, `ValidArgsFunction` and `RegisterFlagCompletionFunc()`. For example:

But any improvement to make this more obvious is welcomed

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