Minimalist approach to wiring up the standard flag package to the environment.
func main() {
listenAddr := flag.String("addr", ":8080", "server listen address")
flag.Parse()
envflag.SetPrefix("myapp")
envflag.Parse()
fmt.Printf("listenAddr is %q\n", *listenAddr)
}
This package connects the standard flag package to the environment with a minimum of ceremony.
12-factor apps store their configuration in the environment. It's also useful, however, to accept command-line flags: they make a program easier to use, and serve as documentation.
package main
import (
"flag"
"fmt"
"os"
"github.com/dcowgill/envflag"
)
func main() {
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
listenAddr := fs.String("addr", ":8888", "server listen address")
timeout := fs.Int("timeout", 10, "client timeout in seconds")
fs.Parse(nil) // simulate no command-line args
// Normally this would happen outside the program.
os.Setenv("MYAPP_LISTEN_ADDR", ":9999")
os.Setenv("MYAPP_TIMEOUT", "42")
vs := envflag.NewVarSet(fs)
vs.SetPrefix("myapp")
vs.RenameFlag("addr", "listen-addr")
vs.Parse()
fmt.Println(*listenAddr) // prints ":9999"
fmt.Println(*timeout) // prints "42"
}