-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Same flag name for different cobra commands does not work #233
Comments
This works around issues where viper gets confused when there are mutiple subcommands with parameters named the same. See also: spf13/viper#233
Any updates on this? |
I think this is the cause:
If the flag was already bound, it simply overwrites it, and it does it silently. In my case I'm doing
so only if I call the second cobra command the flag gets parsed. Otherwise I get the default value |
Having the same issue here. I have a rootCmd that has multiple subcommands. 3 of these subcommands needs the following parameter subcommand.Flags().StringSliceP("param", "p", nil, "Allow to pass multiple parameters to the command")
viper.BindPFlag("param", subcommand.Flags().Lookup("param")) As I have 3 commands using the same flag "param", I tried to use Persistant Flags instead - it would work for me as subcommands does not have subcommands - but without success. Any idea / workaround ? |
Wrote a really fast example case for this while debugging it myself. https://gist.github.com/gesquive/c36501fbd84ecab00d3ff2c9502020f9 Kind of crazy that this bug has been around for a year now. |
Encountered same issue. |
Just ran into this myself too thought I was going crazy, ended up using the workaround in mook-as/fissile@ebd1ad3 and everything is happy again. |
The issue is that init function is executed by go for each command automatically. The following solution allows to us to keep using the global viper, but only register the binds if a command is executed.
See https://github.com/spf13/cobra#prerun-and-postrun-hooks for more details. |
* Move flag bindings to prerun hooks For further information: spf13/viper#233
* Move flag bindings to prerun hooks For further information: spf13/viper#233
This workaround is working for me. Bind the flag again in every subCmd Run: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("user", cmd.Flags().Lookup("user"))
user := viper.GetString("user") |
This is required due to spf13/viper#233.
I got bitten by this too. The documentation/readme should not encourage bad design by abusing I don't call doing this in Bind your viper flags in |
… at the same time. See: spf13/viper#233. The solution is to use Prerun in the Cobra command to bind the viper flags at the last possible moment.
… at the same time. See: spf13/viper#233. The solution is to use Prerun in the Cobra command to bind the viper flags at the last possible moment.
I think viper is abused in this case. Local flags are accessible in the local command thus they're not necessarily stored in viper(cobra's readme is a little misleading). viper should be used to store config and global or "trans-command" parameters to avoid name conflicts. |
… across commands (#2424) ### What Allow multiple flags to be defined with the same name that are attached to different commands. ### Why Cobra allows the definition of flags on different commands that have the same name. There can be good reasons to do this like two subcommands requiring the same parameter, but other commands not requiring that parameter. Viper also supports this too, if we bind flags into viper as they're required, but not if we bind them all upfront. This comment spf13/viper#233 (comment) suggests binding flags to wiper when a command is actually running. This has other advantages, like there are not parameters for other commands bound unnecessarily. Fix #2421
Consolidate config at /config/variables.go Fix linting issues Move bindings to PreRun to workaround spf13/viper#233 Add `Get*` functions to Config Update all GetEnv calls to use config instead
Consolidate config at /config/variables.go Fix linting issues Move bindings to PreRun to workaround spf13/viper#233 Add `Get*` functions to Config Update all GetEnv calls to use config instead
Consolidate config at /config/variables.go Fix linting issues Move bindings to PreRun to workaround spf13/viper#233 Add `Get*` functions to Config Update all GetEnv calls to use config instead
Consolidate config at /config/variables.go Fix linting issues Move bindings to PreRun to workaround spf13/viper#233 Add `Get*` functions to Config Update all GetEnv calls to use config instead
Might be misunderstanding how to apply this workaround
I ran into
The README says
but it doesn't mention this gotcha at all. Does it need an update? |
When using same local flag for different subcommands, binding to the global configuration will erase previous values with the value of the last command's flag registered. See spf13/viper#233. Using a global set of variables to store the flags value of the command solves the issue. Also add a local variable to store the value of the output flag of the tracingpolicy list subcommand. We may want to avoid the usage of the global configuration and using viper.BindPFlags in the future to avoid this issue to appear again. Also add checks on the value of flags that are restricted to certain values. Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
Has someone sent a PR that updates the README? I cannot find any |
When using same local flag for different subcommands, binding to the global configuration will erase previous values with the value of the last command's flag registered. See spf13/viper#233. Using a global set of variables to store the flags value of the command solves the issue. Also add a local variable to store the value of the output flag of the tracingpolicy list subcommand. We may want to avoid the usage of the global configuration and using viper.BindPFlags in the future to avoid this issue to appear again. Also add checks on the value of flags that are restricted to certain values. Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viperremove viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viperremove viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
viper.BindPFlags has a ISSUE spf13/viper#233 replace all viper.BindPFlags Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
got bitten by this as well. any plan on address the issue or at least some docs for the workaround? |
I just spend few hours trying to find why my program did not work. |
- Was initially checking spf13/viper#233 but none of the suggestions there seemed to work. In our case it looked like viper or cobra wasn't loading the value at all. - Moved some code to the PreRun and that seems to work. Some users suggest that the code shouldn't be in the init and use the PreRun instead.
* RHCLOUD-34576 Moving config reading to PreRun function - Was initially checking spf13/viper#233 but none of the suggestions there seemed to work. In our case it looked like viper or cobra wasn't loading the value at all. - Moved some code to the PreRun and that seems to work. Some users suggest that the code shouldn't be in the init and use the PreRun instead. * Adding a test to ensure it doesn't happen again
I'm not sure if this is expected behaviour or not.
I have a
decryptCmd
andencryptCmd
cobra commands.They both have flag
message
.Does
viper.GetString()
work globally across commands or is it command specific? Because when flags have the same name but for different commands then viper can not get the flag value consistently.The text was updated successfully, but these errors were encountered: