cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Function to reparse flags

Open pjebs opened this issue 2 years ago • 3 comments

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.

pjebs avatar Oct 17 '22 23:10 pjebs

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).

johnSchnake avatar Nov 30 '22 18:11 johnSchnake

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

github-actions[bot] avatar Jan 31 '23 00:01 github-actions[bot]

This would be very useful.

jaylevin avatar Feb 12 '24 18:02 jaylevin