Set flags using the standard library flag package, and complement them with environment variables.
Find a file
2026-01-14 09:46:25 +11:00
example_test.go Update docs and examples 2026-01-14 09:46:25 +11:00
flag.go Update docs and examples 2026-01-14 09:46:25 +11:00
flag_test.go Add option to override os.Getenv 2025-08-29 15:36:21 +10:00
go.mod Start envflag 2022-10-27 21:56:07 +11:00
LICENSE Start envflag 2022-10-27 21:56:07 +11:00
README.md Update docs and examples 2026-01-14 09:46:25 +11:00

Environment variables and flags

Set flags using the standard library flag package, and complement them with environment variables.

  • All Go's standard flag functions are used as normal and parse errors are returned as expected.

  • Flags, if set, take precedence over the environment.

  • Environment variables can have custom prefixes and suffixes.

  • Usage output can be displayed with the standard -h flag.

  • Usage output can be annotated with the configured environment variables.

fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.String("db", "", "Database thing")
_ = envflag.ParseFlagSet(fs, envflag.Prefix("APP_"), envflag.UsageSuffixer())

produces the following usage

Usage of test:
  -db string
        Database thing [APP_DB]