An open API service indexing awesome lists of open source software.

https://github.com/zachcheung/eflag

Environment Variable Flag Overrides
https://github.com/zachcheung/eflag

flag go

Last synced: about 1 year ago
JSON representation

Environment Variable Flag Overrides

Awesome Lists containing this project

README

          

# eflag - Environment Variable Flag Overrides

`eflag` is designed to override flag values using environment variables when they are not set explicitly.

Flag value precedence (from greatest to least):

1. explicitly set flag
2. environment variable
3. flag default value

## Installation

```shell
go get github.com/zachcheung/eflag
```

## Usage

```go
package main

import (
"fmt"

"github.com/zachcheung/eflag"
)

func main() {
var (
host string
port int
debug bool
)

eflag.Var(&host, "host", "localhost", "host", "")
eflag.Var(&port, "port", 8000, "port", "-")
eflag.Var(&debug, "debug", false, "debug", "")

eflag.SetPrefix("myapp") // Optionally set prefix for environment variables
eflag.Parse()

fmt.Println("host:", host) // Environment variable "MYAPP_HOST" will overrides default "localhost" if it's set
fmt.Println("port:", port) // Ignores environment variable "MYAPP_PORT"
fmt.Println("debug:", debug) // MYAPP_DEBUG=true vs -debug=false
}
```

## License

[MIT](LICENSE)