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
- Host: GitHub
- URL: https://github.com/zachcheung/eflag
- Owner: zachcheung
- License: mit
- Created: 2024-01-08T03:40:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-23T08:44:44.000Z (over 2 years ago)
- Last Synced: 2024-01-23T10:15:11.482Z (over 2 years ago)
- Topics: flag, go
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)