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

command: Allow overriding of flag parse function #1359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vkareh
Copy link

@vkareh vkareh commented Mar 2, 2021

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.

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.
@CLAassistant
Copy link

CLAassistant commented Mar 2, 2021

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

github-actions bot commented May 2, 2021

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

@marckhouzam
Copy link
Collaborator

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 marckhouzam reopened this Mar 28, 2022
@marckhouzam marckhouzam added kind/feature A feature request for cobra; new or enhanced behavior area/flags-args Changes to functionality around command line flags and args and removed kind/stale labels Mar 28, 2022
@vkareh
Copy link
Author

vkareh commented Apr 4, 2022

@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 openshift/rosa#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args kind/feature A feature request for cobra; new or enhanced behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants