https://github.com/csmith/envflag
Set flags from environment variables in Go
https://github.com/csmith/envflag
Last synced: 5 months ago
JSON representation
Set flags from environment variables in Go
- Host: GitHub
- URL: https://github.com/csmith/envflag
- Owner: csmith
- License: mit
- Created: 2021-10-17T14:12:36.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-11-26T11:57:01.000Z (7 months ago)
- Last Synced: 2025-11-29T10:16:45.316Z (7 months ago)
- Language: Go
- Size: 43.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Yet another `envflag` library
[](https://pkg.go.dev/github.com/csmith/envflag/v2)
This is a simple library that allows that enables reading the value of flags from environment variables.
## Why?
The Go flags package is well-thought-out, battle-tested, and extensively used, but modern
[deployment practices](https://12factor.net/config) tend to favour storing configuration in environment variables rather
than passing them in on the command-line.
`envflag` and its predecessors bridge this gap by mapping environment variables onto flags.
## How?
Import this package, and use `envflag.Parse` instead of `flag.Parse`:
```go
package main
import (
"flag"
"github.com/csmith/envflag/v2"
)
var (
myFlag = flag.String("my-flag", "woohoo", "Something or other")
)
func main() {
envflag.Parse()
println(*myFlag)
}
```
## What?
In its default configuration, `envflag` will:
* Map all flag names to appropriate environment variable names (`my-flag` -> `MY_FLAG`)
* Update the usage information of flags to include the environment variable name
* Show usage and exit with an appropriate status code on error
* Use the default set of flags provided by `flag.CommandLine`
* Call `flag.Parse` to also parse any command-line flags
You can customise the behaviour of `envflag` by passing in options:
### Prefix all environment variables
```go
envflag.Parse(envflag.WithPrefix("MYAPP_"))
```
Changes the mapping of environment variables to always include the given prefix
(e.g. `my-flag` -> `MYAPP_MY_FLAG`).
### Use a different flag set
```go
envflag.Parse(envflag.WithFlagSet(someFlagSet))
```
Use the given `flag.FlagSet` instead of `flag.CommandLine`.
### Don't show environment variable names in usage
```go
envflag.Parse(envflag.WithShowInUsage(false))
```
Suppresses the default behaviour of updating flag usage to include the environment variable names.
### Use a different set of arguments
```go
envflag.Parse(envflag.WithArguments([]string{"-option1", "-option2"}))
```
When parsing the command-line arguments, use the given slice instead of `os.Args[1:]`.
## Licence/credits/contributions etc
Released under the MIT licence. See LICENCE for full details.
Heavily inspired by [kouhin/envflag](https://github.com/kouhin/envflag) which does
mostly the same job but can't easily add prefixes and is a bit harder to configure.
Contributions are welcome! Please feel free to open issues or send pull requests.