cobra icon indicating copy to clipboard operation
cobra copied to clipboard

command: Allow overriding of flag parse function

Open vkareh opened this issue 3 years ago • 4 comments

To allow setting a user-defined flag parser, we add a new SetFlagParseFunc to Command. This function, when set, will be called instead of pflag.Parse.

vkareh avatar Mar 02 '21 16:03 vkareh

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 02 '21 16:03 CLAassistant

This PR is being marked as stale due to a long period of inactivity

github-actions[bot] avatar May 02 '21 00:05 github-actions[bot]

Thanks for the contribution @vkareh, sorry it hasn't been reviewed yet. This seems like an interesting suggestion but I'm not very knowledgeable on the feature of pflags, so I would need to dig deeper into it. I'll re-open.

@vkareh could you give a concrete example of what you would be able to do with this new support?

marckhouzam avatar Mar 28 '22 12:03 marckhouzam

@marckhouzam thanks for re-opening this. The way we would use this is to allow commands to accept flags that come from outside of the code.

As a concrete example https://github.com/openshift/rosa/pull/272 would use this to allow the program to accept arbitrary flags that are sent to an external API, but still allow parsing the flags that are part of our program. Today we have to disable flag parsing, override the PositionalArgs with our own function, then inside theh Run call we need to manually parse the rest of the flags.

Having the ability to override the flag parsing function can get rid of a lot of that by just calling our function directly, then letting cobra handle the rest.

vkareh avatar Apr 04 '22 19:04 vkareh