Setting ZERO value conflicts with flag processing libraries.
Setting ZERO value conflicts with flag processing libraries.
Imagine that you want your app to support both flags and env vars. First you want your flags to be processd by flags package and then by env package.
Your library will overwrite all vars which were not providen through environment with zero values.
Are you considering this case?
Example which will not work:
var appFlags = Flags{}
func init() {
// parse command line
if _, err := flags.Parse(&appFlags); err != nil {
os.Exit(1)
}
// parse enviroment
if err := env.Process(&appFlags); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
I hadn't considered this case as I don't tend to use any flags, however what you've posted makes a lot of sense and is probably what a fair bit of people are trying to do. I'll work this out. Shouldn't be too hard to hammer out.
Could you please include what the Flags structure looks like from your example?
Hi Dan, please observe. For simplicity i use go-flags as very good flags package replacement.
package main
import (
"os"
"github.com/jessevdk/go-flags"
)
type Flags struct {
KeyFile string `long:"key-file" description:"Path to a private key" required:"yes"`
CertFile string `long:"cert-file" description:"Path to a certificate" required:"yes"`
CaFile string `long:"ca-file" description:"Path to a CA certificate" required:"yes"`
Port string `short:"p" long:"port" description:"Port to listen on" default:"3001" env:"key=PORT"`
}
var appFlags = Flags{}
func init() {
if _, err := flags.Parse(&appFlags); err != nil {
os.Exit(1)
}
if err := env.Process(&appFlags); err != nil {
fmt.Println(err)
os.Exit(1)
}
}