https://github.com/loderunner/popt
A collection of helper functions for viper and pflag
https://github.com/loderunner/popt
Last synced: 8 months ago
JSON representation
A collection of helper functions for viper and pflag
- Host: GitHub
- URL: https://github.com/loderunner/popt
- Owner: loderunner
- License: other
- Created: 2019-02-04T20:50:14.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-04T21:47:15.000Z (about 7 years ago)
- Last Synced: 2025-06-19T11:43:02.442Z (9 months ago)
- Language: Go
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# popt
[](https://godoc.org/github.com/loderunner/popt)
Package popt defines a series of helpers to define options for pflag and viper in one place.
## Option usage
Use Option to define configuration options for your program. This allows quick and consistent bindings for
configuration file options, flags and environment variables.
Start by configuring an option for your program.
```go
var nameOption = popts.Option{
Name: "name",
Default: "World",
Usage: "the name of the person you wish to greet",
Env: "HELLO_NAME",
Flag: "name",
Short: "n",
}
```
At init time, add the option to your program.
```go
func init() {
// Add name option
if err := popt.AddOption(nameOption, pflag.CommandLine); err != nil {
panic(err)
}
}
```
When running your executable, bind your option and parse the flags.
```go
func main() {
// Bind env var and flag to viper
popt.BindOption(nameOption, pflag.CommandLine)
// Parse command-line flags
pflag.Parse()
// Read configuration file
viper.SetConfigName("hello")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.ReadInConfig()
fmt.Println("Hello", viper.GetString(nameOption.Name))
}
```
Example usage:
```
$ ./hello
Hello World
$ ./hello -h
Usage of ./hello:
-n, --name string the name of the person you wish to greet (default "World")
pflag: help requested
$ ./hello --name="Steve"
Hello Steve
$ HELLO_NAME="Brooklyn" ./hello
Hello Brooklyn
$ HELLO_NAME="Brooklyn" ./hello --name="Steve"
Hello Steve
$ echo 'name: "Sunshine"' > hello.yaml
$ ./hello
Hello Sunshine
```
You can also define your configuration options in a JSON file, to be loaded at runtime.
`options.json`
```json
[
{
"name": "address",
"default": "localhost",
"usage": "The address of the remote host",
"flag": "address",
"short": "a",
"env": "HELLO_ADDRESS"
},
{
"name": "port",
"default": 8080,
"usage": "The port of the remote host",
"flag": "port",
"short": "p",
"env": "HELLO_PORT"
},
{
"default": false,
"usage": "Make the operation more talkative",
"flag": "verbose",
"short": "v"
}
]
```
Main
```go
func main() {
// error handling omitted for brevity
f, err := os.Open("options.json")
data, err := ioutil.ReadAll(f)
var opts []popt.Option
err = json.Unmarshal(data, &opts)
err = popt.AddAndBindOptions(opts, pflag.CommandLine)
pflag.Parse()
}
```
Usage
```
$ ./popt_json -h
Usage of ./popt_json:
-a, --address string The address of the remote host (default "localhost")
-p, --port string The port of the remote host (default "8080")
-v, --verbose Make the operation more talkative
pflag: help requested
```
CAVEAT: Beware that your options defined in JSON will follow JSON typing; in particular, all your numbers will be
float64s.
## License
```
Original work, Copyright 2017 Pantomath SAS
Modified work, Copyright (c) 2019 Charles Francoise
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```