Skip to content

Add feature to allow ValidArgsFunction to be called when processing command line completions of just flags #2245

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

fnickels
Copy link
Contributor

@fnickels fnickels commented Mar 4, 2025

Addresses issue raised in #2243

Implements a new Command field AllowCustomFlagCompletions (I am not married to the name.) which when set, and when a Command has a ValidArgsFunction set, will call the ValidArgsFunction function when processing flag command line completions instead of getCompletions()'s default processing of flags.

Currently when Cobra is processing command line completions which start with "-" getCompletions() handles assembling the list of return values solely based on the flags populating the current command and does not make a call to the command's .ValidArgsFunction. In most use cases, this is perfectly adequate.

Yet, if as a developer of a CLI you wanted to suppress certain flags based upon criteria other than the state of other flags, the current implementation does not appear to provide a mechanism to customize the completion results. Allowing getCompletions() to override it's default behavior and call out to ValidArgsFunction, as it does for all other conditions, would remove this limitation of functionality.

The value of this feature is it allows a developer more fine grain control over command line completion results. Specifically flags based upon non-flag based context. (ie: allows for the suppression of a flag that may not be applicable when a particular argument is present, or when a certain number of arguments are present.)

I am not well versed with the completion code base here and would suggest this be reviewed by others who are more familiar.

Copy link

github-actions bot commented Mar 4, 2025

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

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 this pull request may close these issues.

1 participant