envflag
Package envflag
is a Go package that
exposes environment variables for all the flags within
a flag.FlagSet
.

It came out of a response to a
Reddit post
by jalquiza
about their envy package.
This package differs from that one in several ways:
the usage is slightly different, it is not a drop in replacement
it can be used with any flag.FlagSet
, not just flag.CommandLine
it tries harder (compared to the original envy
) to avoid effecting
the reported argument defaults or the results of functions/methods such as
flag.Visit
and flag.NFlag
if there are any errors parsing environment variables (e.g. a string value where
an interger value is expected) the first such encountered is returned
See the
GoDoc documentation
for complete examples but basically you can just replace the call to:
flag.Parse()
with:
envflag.Parse("PROGNAME", true)
to have envirment variables such as PROGNAME_PORT
be used for
the program’s -port
argument when no such argument is provided.
Copyright and License
This package is Copyright 2017, Dave Chapeskie.
You are free to do with it anything you want, with the sole exception
being you cannot replicate this package and claim you wrote it.