go get github.com/nochso/narg
See godoc for API docs and examples.
Given a configuration struct consisting of scalar types, structs and slices:
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
:
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:
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.
This package is released under the MIT license.