https://github.com/nochso/narg
https://github.com/nochso/narg
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nochso/narg
- Owner: nochso
- License: mit
- Created: 2017-03-11T19:48:53.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-15T09:11:59.000Z (almost 9 years ago)
- Last Synced: 2024-06-20T17:53:34.169Z (almost 2 years ago)
- Language: Go
- Size: 47.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
narg - Nested arguments as configuration
========================================
[](LICENSE)
[](https://github.com/nochso/narg/releases)
[](http://godoc.org/github.com/nochso/narg)
[](https://goreportcard.com/report/github.com/nochso/narg)
[](https://travis-ci.org/nochso/narg)
[](https://coveralls.io/github/nochso/narg?branch=master)
Installation
------------
go get github.com/nochso/narg
Documentation
-------------
See [godoc](https://godoc.org/github.com/nochso/narg) for API docs and examples.
### Decoding configuration
Given a configuration struct consisting of scalar types, structs and slices:
```go
type testConf struct {
Name string
Port int
Debug bool
Float float32
Hosts []string
Ports []int
PortsU []uint8
Admin testUser
Users []testUser
}
type testUser struct {
ID int `narg:"0"`
Name string `narg:"1"`
}
```
Note that users can be defined using positional arguments instead of named ones.
The order of the arguments is defined using the struct tag `narg` with the
zero-based index (see testUser struct above).
The following narg input can be decoded into a pointer to `testConf`:
```go
in := `name foo
port 80
debug 1
float 3.14
hosts a bee "cee e"
ports 1024 1025
portsu 0xff 255
admin {
id 1
name "Phil \"Tandy\" Miller"
}
users {
id 2
name "Carol Pilbasian"
}
users 4 Todd`
cfg := &testConf{}
err := Decode(strings.NewReader(in), cfg)
```
Field names are case-insensitive.
`cfg` has now been populated with the given narg input:
```go
cfg = &testConf{
Name: "foo",
Port: 80,
Debug: true,
Float: 3.14,
Hosts: []string{"a", "bee", "cee e"},
Ports: []int{1024, 1025},
PortsU: []uint8{0xff, 0xff},
Admin: testUser{
ID: 1,
Name: `Phil "Tandy" Miller`,
},
Users: []testUser{
{2, "Carol Pilbasian"},
{4, "Todd"},
},
}
```
Currently only supplied values are overwritten. You can set up your struct with
sane defaults and when decoding a sparse config the defaults will be kept.
License
-------
This package is released under the [MIT license](LICENSE).