viper: Same flag name for different cobra commands does not work
I’m not sure if this is expected behaviour or not.
I have a decryptCmd and encryptCmd cobra commands.
They both have flag message.
func init() {
RootCmd.AddCommand(encryptCmd)
encryptCmd.Flags().StringP("message", "m", "teststring", "secret unencrypted base64")
viper.BindPFlags(encryptCmd.Flags())
}
func init() {
RootCmd.AddCommand(decryptCmd)
decryptCmd.Flags().StringP("message", "m", "teststring", "base64 encrypted secret")
viper.BindPFlags(decryptCmd.Flags())
}
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.
About this issue
- Original URL
- State: open
- Created 10 years ago
- Reactions: 47
- Comments: 16
Commits related to this issue
- Use sub-vipers for command-specific flags This works around issues where viper gets confused when there are mutiple subcommands with parameters named the same. See also: https://github.com/spf13/vip... — committed to mook-as/fissile by mook-as 10 years ago
- Fix not working flags in generate * Move flag bindings to prerun hooks For further information: https://github.com/spf13/viper/issues/233 — committed to mrclmr/icm by deleted user 8 years ago
- Fix not working flags in generate * Move flag bindings to prerun hooks For further information: https://github.com/spf13/viper/issues/233 — committed to mrclmr/icm by deleted user 8 years ago
- go: Re-bind flags to viper for each sub-command This is required due to https://github.com/spf13/viper/issues/233. — committed to oasisprotocol/oasis-core by kostko 8 years ago
- fix: Output flag viper binding is overwritten if used by two commands at the same time. See: https://github.com/spf13/viper/issues/233. The solution is to use Prerun in the Cobra command to bind the v... — committed to Blokje5/conftest by Blokje5 7 years ago
- fix: Output flag viper binding is overwritten if used by two commands at the same time. See: https://github.com/spf13/viper/issues/233. The solution is to use Prerun in the Cobra command to bind the v... — committed to Blokje5/conftest by Blokje5 7 years ago
- support/config: allow multiple flags to be defined with the same name across commands (#2424) ### What Allow multiple flags to be defined with the same name that are attached to different commands... — committed to stellar/go by leighmcculloch 7 years ago
- tools/istio-iptables: allow `dry-run` flag to be used across commands The `dry-run` flag is bound in init() across both `istio-iptables` and `istio-clean-iptables` subcommands and will be overwritten... — committed to kailun-qin/istio by kailun-qin 6 years ago
- tools/istio-iptables: allow `dry-run` flag to be used across commands (#30710) The `dry-run` flag is bound in init() across both `istio-iptables` and `istio-clean-iptables` subcommands and will be ov... — committed to istio/istio by kailun-qin 6 years ago
- yugaware-client: Fix flag binding This fixes an issue where common flag names conflict with each other when binding to viper. More information can be found here: https://github.com/spf13/viper/issu... — committed to yugabyte/yb-tools by tvesely 5 years ago
- Avoid viper bug regarding flag name collisions This issue on github explains the problem: https://github.com/spf13/viper/issues/233#issuecomment-386791444 The first collision we noticed was between ... — committed to mariadb-corporation/skysql-api-cli by mariadb-DonMayo 5 years ago
- Avoid viper bug regarding flag name collisions This issue on github explains the problem: https://github.com/spf13/viper/issues/233#issuecomment-386791444 The first collision we noticed was between ... — committed to mariadb-corporation/skysql-api-cli by mariadb-DonMayo 5 years ago
- Fix reuse of flags in different commands - https://github.com/spf13/viper/issues/233 — committed to posilva/hdrcrypto by posilva 5 years ago
- Fix reuse of flags in different commands - https://github.com/spf13/viper/issues/233 — committed to posilva/hdrcrypto by posilva 5 years ago
- Fix reuse of flags in different commands - https://github.com/spf13/viper/issues/233 — committed to posilva/hdrcrypto by posilva 5 years ago
- Move viper.BindPFlag to PreRun instead of init Summary: Viper clobbers flags which means that subcommands cannot share flags since the last subcommand to have it's init called will be the only one th... — committed to pixie-io/pixie by vihangm 4 years ago
- Move viper.BindPFlag to PreRun instead of init Summary: Viper clobbers flags which means that subcommands cannot share flags since the last subcommand to have it's init called will be the only one th... — committed to orishuss/pixie by vihangm 4 years ago
- Move viper.BindPFlag to PreRun instead of init Summary: Viper clobbers flags which means that subcommands cannot share flags since the last subcommand to have it's init called will be the only one th... — committed to orishuss/pixie by vihangm 4 years ago
- Move viper.BindPFlag to PreRun instead of init Summary: Viper clobbers flags which means that subcommands cannot share flags since the last subcommand to have it's init called will be the only one th... — committed to orishuss/pixie by vihangm 4 years ago
- fix: move viper binding to PreRun (https://github.com/spf13/viper/issues/233) — committed to thpiron/webex-helper by thpiron 4 years ago
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.
I got bitten by this too. The documentation/readme should not encourage bad design by abusing
init()functions like this. This doesn’t work because you are mutating Viper configuration and binding options to keys in viper’s state that are dynamically bound to the sub-command being executed. Doing it ininit()seems wrong here.I don’t call doing this in
PreRiun()a “work-around” at all; but the correct way to bind viper flags (for lack of any other documented accepted way).Bind your viper flags in
PreRun()orRuin() -- **NOT**init()`.Just ran into this myself too thought I was going crazy, ended up using the workaround in https://github.com/mook-as/fissile/commit/ebd1ad311ea40b474688182d5ce9babd88d163e0 and everything is happy again.
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.
This workaround is working for me. Bind the flag again in every subCmd