-
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
BindPFlag don't work properly with pflag.StringSlice #112
Comments
Just hit this too. For now I just replaced that flag to a comma-delimited string and just strings.split(), kinda dirty but it works. |
Got that problem too. |
I was debuging this problem too (few hours searching how to use multiple times same flag with viper, then few hours to find what is wrong) and have found that you can use (get) only int, bool or string type with viper if you bind flag to it. In code there is few places, where it goes wrong and also there is few re-casting to the same value and same type (if int cast to int...). If you want to work with flags slices, do it without viper, like (i am using cobra, serverCmd is cobra.Command):
Viper only call |
Yes, thank you @Morlinest for your answer. I use Cobra as well, and this was working nicely (same strategy as yours). I faced that issue when I tried to bind values from a configuration file, as it is possible with Viper. I think I'll follow this post and use a simple list of comma separate string in the meantime, as suggested by @AdmiralObvious |
Yes, using list of "x" separate string is good. I am already using it (using ":" as separator) but also needs more flags of same type (using another separator like "|" would be mess :D). Some sugestions to get it work (dont know meaning of all this code and where its used and dont have time to work on it myself right now):
|
Regarding separators, I suggest the creation of the environment variable $VIFS, as Viper's version of Bash's $IFS. Defaulting to a FWIW, does #110 and its merged PR #111 help at all? I ran into this issue for the exact same reason as well, that PFlag does funky things when emitting arrays as strings (prefixing and suffixing the contents with a bracket) such that later attempts to read the data made it impossible. |
Thanks, for my purpose your patch does not help. I am not using default values nor structs to hold pure flag/config data (want to use viper as "store"...) or to work with them. In this situation, there will be still same issue because viper can get flag value only as string and then cast it to int/bool/string and if you set You are right about implementation of stringslice in "string_slice.go" (github.com/spf13/pflag) - |
Hi @Morlinest, What if you use a default value simply to init the default type? It's a band-aid, but maybe it could help in the short-term? An empty string slice for example. |
Hi @akutz, dont think so as i said:
Using type from default value is last part of |
* Read configuration from environment, flags and config Also add an initial implementation of clean shutdown via context Also handle SIGTERM properly Also add JSON logging * Setup logger ASAP Each step ensures maximum available configuration is applied before the next potentially failing step is executed. So if it fails, it will log the error with the correct logging format and verbosity. * Workaround spf13/viper#112 * Simplify exit function
With some extra trace log formatting changes thrown into the mix. Fixes spf13#112
With some extra trace log formatting changes thrown into the mix. Fixes spf13#112
Fixes #112 Changes some logging directives to use Printf
`pflag.StringArray` suffers from the same problems as `StringSlice` did described in this issue spf13#112
Hopefully build/push from travis Refactor DefaultAuth to prevent accidentally implemented race conditions Make maps thread safe by implementing mutexes Remove patch for spf13/viper#112 (:fingerscrossed: all works) Fix simultaneous connection handlers
An open and close bracket are added to the first and last item of a StringSlice when it's updated by a flag.
Here's some test code that demonstrates the issue:
And here's the output:
The text was updated successfully, but these errors were encountered: