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 flags after execute #1784

Closed
theprobugmaker opened this issue Aug 21, 2022 · 3 comments
Closed

Add flags after execute #1784

theprobugmaker opened this issue Aug 21, 2022 · 3 comments

Comments

@theprobugmaker
Copy link

Is it possible to add flags to the command after the execution? I want to dynamically add more flags to the application.

The problem I'm having is that those additional flags will only be added if a flag --extra-features is present and I only have the value filled inside the RunE function.

If this --extra-features is present I want to add more flags to the application. How can I do something like that?

Thank you.

@theprobugmaker
Copy link
Author

Does anyone know if this is possible?

@marckhouzam
Copy link
Collaborator

Hi @theprobugmaker .
I haven't tried but here is my educated guess.

Since you want to add flags you need to do this beforeParseFlags() is called. I see two options:

  1. before calling Execute(), look for --extra-features in os.Args yourself, and add your new flags accordingly, or
  2. Enable DisableFlagParsing on the root command and parse the flags yourself in one of the *Run() functions by first looking for for --extra-features manually, adding the new flags and then calling ParseFlags() yourself. The __complete command does something a bit similar in completions.go

In your case, which is simpler than for completions, I would use the first option, personally.

I hope this helps.

@theprobugmaker
Copy link
Author

theprobugmaker commented Aug 24, 2022

@marckhouzam Thank you so much for taking the time to answer me, I appreciate it.
I just found a similar issue #1758 and I tried to do the same, disabling DisableFlagParsing, parsing myself, adding more flags, enabling DisableFlagParsing again, and calling the parse again (just like he did) and it "worked". But I noticed that there are way more things that Execute does such as validation, showing the help if no argument is found, and more (I kinda feel like recreating the Execute method).
He created a PR to expose validation methods: #1760 (btw).
The first approach is the easiest, I thought about that but I needed to ask to see if there's another way (it would be cool if this could be easier using the library itself).

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