cobra
cobra copied to clipboard
Function to reparse flags
It would be good for Cobra to have a function to force reparsing for flags. eg. after the application has already started and the initial flag parsing has already been done.
var reverse bool
cmd.Flags().BoolVarP(&reverse, "reverse", "r", false, "")
cmd.ParseFlags(args) // Neither work
cmd.Flags().Parse(args) // Neither work
My use case is a bit weird. I'm assuming most people's applications execute a command and then exit immediately.
In my case, the application continually runs. It executes commands, and then awaits a new command and loops.
Cobra seems to always remember the original flag values and never updates them when you run the command again with a different flag value.
Is this for testing? I recall when I was needing that sort of functionality the best thing to do is simply design the tests so that you get a new instance of the command/flags/etc otherwise you tend to remain exposed to small bugs in the implementation anyways. I think the issue is that in order to have an automatic 'ResetFlags' option, that means that cobra would have to be recording the default values/saving previous values and for custom flag types it may not make any sense (since a custom Set function could have sideeffects and not be able to be reset in a generic way).
The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:
- After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed. You can:
- Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interested in reopening
This would be very useful.