-
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
StringSlice behavior different for command line flag and environment variable #380
Comments
Another example, which shows environment variables are handled differently. Notice, the last examlpe shows if the environment variable values are separated by spaces then a slice is correctly populated. Here's the code:
If you build it (name the binary
|
This looks like it could use some movement; is anyone working on it? |
I ran into the same issue and demonstrated it with this test case.
Further research, the code in pflag string_slice.go that parses StringArray is:
Viper uses the cast library, which only splits on runes that return true for IsSpace() I think this hasn't been addressed because viper uses the cast library in many places, and the correct fix is probably to extract a common lib that both viper and pflag use to ensure the same behavior. It seems to be more than a 5 minute fix. |
Any updates on this issue? |
The behavior seems inconsistent between the flags and the env, as evidenced by the following: package main
import (
"log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func main() {
viper.SetEnvPrefix("foo")
viper.AutomaticEnv()
pflag.StringSlice("strarr", []string{}, "")
viper.BindPFlags(pflag.CommandLine)
pflag.Parse()
strs := viper.GetStringSlice("strarr")
log.Println(len(strs), strs)
} Then running these yields the following:
|
The case becomes more annoing if you use env variables from .env and run the service with docker-compose which according to docker docs does not respect any kind of qotation for values |
Man is this still a thing |
Wow, this has been ignored for a looooong time. And still in effect. |
- Fixed an issue in the config command where the `viper.BindPFlag` error caused the parsing of `git.exclude_list vendor/*,go.sum,**/code_gen.go` as `"exclude_list":"vendor/*,go.sum,**/code_gen.go"` and writing it to `.codegpt.yaml`, resulting in the global exclude_list being ineffective.(spf13/viper#380) - Fixed a typo in the comments: `srvice` -> `service`.
- Fixed an issue in the config command where the `viper.BindPFlag` error caused the parsing of `git.exclude_list vendor/*,go.sum,**/code_gen.go` as `"exclude_list":"vendor/*,go.sum,**/code_gen.go"` and writing it to `.codegpt.yaml`, resulting in the global exclude_list being ineffective.(spf13/viper#380) - Fixed a typo in the comments: `srvice` -> `service`.
This is still an issue, sigh! |
2024 😢 |
Unfortunately, it's challenging to change a default behavior without someone calling it a regression. Chances are, someone relies on the current behavior. Here is a workaround that you could use (based on the example of the reporter): package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var RootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
+ var h []string
+ viper.UnmarshalKey("hosts", &h)
- h := viper.GetStringSlice("hosts")
fmt.Printf("hosts: %+v (type: %T, length: %d)\n", h, h, len(h))
},
}
func init() {
viper.SetEnvPrefix("test")
viper.AutomaticEnv()
RootCmd.PersistentFlags().StringSlice("hosts", []string{}, "list of hosts")
viper.BindPFlag("hosts", RootCmd.PersistentFlags().Lookup("hosts"))
viper.SetDefault("hosts", []string{})
}
func Execute() {
RootCmd.Execute()
}
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} I know a lot of people like using the I also opened a more generic issue to track this: #1744 I hope the above helps. |
+1 |
The behavior is different for
StringSlice
if an argument is on the command line flag compared to an environment variable. The flag is parsed correctly, but the environment variable is not.Here is an example to see the behavior:
Here is the output:
The text was updated successfully, but these errors were encountered: