Add feature to allow ValidArgsFunction to be called when processing command line completions of just flags #2245
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.