Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amenzhinsky/flagenv
Environment variables support for the snadard flag package.
https://github.com/amenzhinsky/flagenv
cli command-line environment-variables go golang
Last synced: 2 days ago
JSON representation
Environment variables support for the snadard flag package.
- Host: GitHub
- URL: https://github.com/amenzhinsky/flagenv
- Owner: amenzhinsky
- License: mit
- Created: 2019-05-30T11:38:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-27T19:01:36.000Z (over 4 years ago)
- Last Synced: 2024-06-20T19:19:04.270Z (5 months ago)
- Topics: cli, command-line, environment-variables, go, golang
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flagenv
Environment variables support for the standard `flag` package.
## Usage
The library helps to reduce amount of code usually written to use environment variables as fallbacks for command-line options.
Simply replace `flag.Parse()` in your main application with `flagenv.Parse()` to add the functionality (the module also supports passing `*flag.FlagSet` to `flagenv.ParseWithEnv` for lower-level usage).
Environment is only applied to the flags that are not set through the command-line arguments.
Environment variable names are added to each flag in the help message:
```
Usage of main:
-timeout uint
connection timeout [$TIMEOUT] (default 10)
```## Configuration
In case you're using short flag names you may employ `WithMap` option to override environment variable names with more descriptive alternatives:
```go
flag.StringVar(&addrFlag, "a", ":8080", "address to connect to")
flag.UintVar(&timeoutFlag, "t", 0, "connection timeout")
flag.BoolVar(&forceFlag, "f", false, "force the action")
flagenv.Parse(flagenv.WithMap(func(name string) string {
switch name {
case "a":
return "ADDR"
case "t":
return "TIMEOUT"
case "f":
// ignore '-f' flag
return ""
default:
// fall back to the default behaviour
return flagenv.DefaultMap(name)
}
}))
```## Contributing
All contributions are welcome. Please fill in an issue before submitting pull requests.