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

Add getters for flagCompletionFunctions map to support interactive flag completion suggestions in cobra-prompt #1942

Closed
avirtopeanu-ionos opened this issue Mar 30, 2023 · 6 comments

Comments

@avirtopeanu-ionos
Copy link
Contributor

avirtopeanu-ionos commented Mar 30, 2023

The flagCompletionFunctions map (https://github.com/spf13/cobra/blame/main/completions.go#L36) is used to store flag completion functions for a given command

I am working on extending the cobra-prompt library (https://github.com/stromland/cobra-prompt/) to add support for interactive flag completion suggestions. To achieve this, I need to access the flagCompletionFunctions map and retrieve the flag completion functions for a given command.

@avirtopeanu-ionos
Copy link
Contributor Author

I made a PR #1943 PTAL at the proposal and tell me if you're okay with it

@marckhouzam
Copy link
Collaborator

Thanks @avirtopeanu-ionos. Could you give an example of where you would need to access the existing registered flag completions?

I believe you should already be able to add new flag completions to another program. And I guess if you want to know if your flag completion already exists, you could try to register a new one and check for an error being returned?

@avirtopeanu-ionos
Copy link
Contributor Author

avirtopeanu-ionos commented Mar 30, 2023

I believe you should already be able to add new flag completions to another program. And I guess if you want to know if your flag completion already exists, you could try to register a new one and check for an error being returned?

I may be misunderstanding something here. I don't want to add suggestions to the cobra command itself, I want to add the existing cobra suggestions from flagCompletionFunctions to the embedded go-prompt Document object, which the cobra-prompt library is building here https://github.com/stromland/cobra-prompt/blob/main/cobra-prompt.go#L131

Could you give an example of where you would need to access the existing registered flag completions?

If PR #1943 were to be merged, then this would add flag value completions to a go-prompt document object:

flagName := d.GetWordBeforeCursorWithSpace()
if compFunc, exists := command.GetFlagCompletionByName(flagName); exists { // Here I call the func in PR #1943 
	completions, _ := compFunc(command, args, flagName)

	for _, completion := range completions {
		suggestions = append(suggestions, prompt.Suggest{Text: completion}) // Here is where I'm building the go-prompt suggestions
	}
}

Here is my fork of cobra-prompt which uses PR #1943 for the getters https://github.com/avirtopeanu-ionos/cobra-prompt/tree/feat/add-flag-completions

@avirtopeanu-ionos avirtopeanu-ionos changed the title How to read from flagCompletionFunctions? feature request: getters for flagCompletionFunctions map Apr 5, 2023
@avirtopeanu-ionos avirtopeanu-ionos changed the title feature request: getters for flagCompletionFunctions map Add getters for flagCompletionFunctions map to support interactive flag completion suggestions in cobra-prompt Apr 18, 2023
@avirtopeanu-ionos
Copy link
Contributor Author

@marckhouzam I have edited my previous messages to hopefully provide more clarity to the described use case. By adding getters for the flagCompletionFunctions map, we can easily extend cobra and integrate it with completion/QOL packages like cobra-prompt.

I hope this clears up any confusion. If you have any questions or concerns about this issue or PR #1943 please let me know

@avirtopeanu-ionos
Copy link
Contributor Author

Any update on this? We were considering making a fork of cobra if this PR #1943 shouldn't be merged

@avirtopeanu-ionos
Copy link
Contributor Author

#1943 was merged. Thank you!

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