Compare commits

...

2 commits

Author SHA1 Message Date
Miguel Elias dos Santos 10816aec9a
Merge dc20f95f1f into b77f4c1928 2023-08-08 22:13:11 -04:00
Miguel Elias dos Santos dc20f95f1f Add example regarding required flags with Cobra + Viper 2020-02-25 17:46:37 +11:00

View file

@ -298,6 +298,16 @@ os.Setenv("SPF_ID", "13") // typically done outside of the app
id := Get("id") // 13
```
#### When working with *required flags with Cobra+Viper*, you must set the flag values after binding to them. Otherwise Cobra won't be aware of the value from any source other tha the CLI itself.
```go
serverCmd.Flags().VisitAll(func(f *pflag.Flag) {
if viper.IsSet(f.Name) && viper.GetString(f.Name) != "" {
serverCmd.Flags().Set(f.Name, viper.GetString(f.Name))
}
})
```
### Working with Flags
Viper has the ability to bind to flags. Specifically, Viper supports `Pflags`