A simple go module to manage flags and env variables, and get them as needed.
Supports Generics
- Get flags and environment variables
- Specify default values
- Specify a custom struct to fill
- Specify preference, whichever value will be used first
package main
import (
"fmt"
"github.com/maxkruse/flagorenv"
)
type ExampleStruct struct {
StringField string `default:"my default string, as per the tag"`
IntField int64
StrArr []string
}
func main() {
c, err := flagorenv.LoadFlagsOrEnv[ExampleStruct](&flagorenv.Config{
Prefix: "test",
PreferFlag: true,
})
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", c)
}
And calling the program like such
TEST_INT_FIELD=420 go run example.go -test-int-field 69 -test-str-arr "hi,bye,good"
will result in the following output:
{StringField:my default string, as per the tag IntField:69 StrArr:[hi bye good]}
go get -u github.com/maxkruse/flagorenv
If you pass both the Environment-Variable and the Flag, the config's PreferFlag
determines which value will be used.
If you pass only one of the two, the value will be used.
If you omit the value, the default value will be used. Either using go-default's tags, or Zero-initialized.
As of right now, only the following types are supported, any others will log an error:
- string
- int64
- float64
- bool
- []string (separated by comma)
- go-strcase For converting strings interally to either SNAKE_CASE (env vars) or kebab-case (flags)