Skip to content
New issue

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

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

Already on GitHub? # to your account

Completion helper #2222

Closed
ccoVeille opened this issue Jan 22, 2025 · 3 comments · Fixed by #2231
Closed

Completion helper #2222

ccoVeille opened this issue Jan 22, 2025 · 3 comments · Fixed by #2231

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

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

Successfully merging a pull request may close this issue.

2 participants