https://github.com/ngdinhtoan/flagstruct
A simple way to register and parse flag into struct
https://github.com/ngdinhtoan/flagstruct
flagset golang parse register
Last synced: 5 months ago
JSON representation
A simple way to register and parse flag into struct
- Host: GitHub
- URL: https://github.com/ngdinhtoan/flagstruct
- Owner: ngdinhtoan
- License: mit
- Created: 2015-09-28T18:21:55.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-05-10T07:05:39.000Z (about 8 years ago)
- Last Synced: 2025-07-29T03:29:44.326Z (11 months ago)
- Topics: flagset, golang, parse, register
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FlagStruct
*A simple way to register and parse flag into struct*
[](https://travis-ci.org/ngdinhtoan/flagstruct)
[](https://coveralls.io/github/ngdinhtoan/flagstruct)
[](https://goreportcard.com/report/ngdinhtoan/flagstruct)
[](https://godoc.org/github.com/ngdinhtoan/flagstruct)
## Install
go get -u github.com/ngdinhtoan/flagstruct
## Tag syntax
`flag:"name" default:"value" usage:"description"`
Tag `default` and `usage` can be omit.
## Example
```go
package main
import (
"fmt"
"github.com/ngdinhtoan/flagstruct"
)
type dbConfig struct {
Hostname string `flag:"hostname" default:"localhost" usage:"Hostname"`
Port uint64 `flag:"port" default:"3306"`
DbName string `flag:"db_name" usage:"Database name"`
}
func main() {
conf := dbConfig{}
flagstruct.Parse(&conf)
fmt.Println("Hostname:", conf.Hostname)
fmt.Println("Port:", conf.Port)
fmt.Println("DB Name:", conf.DbName)
}
```
Run with some options:
go run main.go -hostname=127.0.0.1 -db_name=test_db
Output:
Hostname: 127.0.0.1
Port: 3306
DB Name: test_db