Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for []int []string []float32 arguments #1

Merged
merged 1 commit into from Dec 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions example_app/example.go
@@ -1,14 +1,17 @@
package main

import (
"log"
"github.com/Babazka/uniconfig"
"log"
)

type MyConfig struct {
Debug bool
Count int `help:"number of items"`
Nested1 struct {
Debug bool
Count int `help:"number of items"`
Intlist []int
Strlist []string
Floatlist []float64
Nested1 struct {
A string
B string
ignored string
Expand All @@ -19,7 +22,7 @@ type MyConfig struct {
}

func main() {
config := &MyConfig{}
config := &MyConfig{Intlist: []int{1, 2, 3}}
config.Count = 42
uniconfig.Load(config)
log.Printf("Hi")
Expand Down
40 changes: 40 additions & 0 deletions floatslice.go
@@ -0,0 +1,40 @@
package uniconfig

import (
"fmt"
"reflect"
"strconv"
"strings"
)

type floatslice struct {
data *[]float64
}

func NewFloatSlice(data *[]float64) *floatslice {
return &floatslice{data}
}

func (i *floatslice) String() string {
return fmt.Sprintf("%d", *i.data)
}

// The second method is Set(value string) error
func (i *floatslice) Set(value string) error {
tmp := []float64{}
res := strings.Split(value, ",")
for _, p := range res {
p1 := strings.TrimSpace(p)
val, err := strconv.ParseFloat(p1, 64)
if err != nil {
return fmt.Errorf("Cannot parse %s as []float64\n", value)
}

tmp = append(tmp, val)
}
*i.data = tmp

return nil
}

var floatSliceType = reflect.TypeOf([]float64{})
40 changes: 40 additions & 0 deletions intslice.go
@@ -0,0 +1,40 @@
package uniconfig

import (
"fmt"
"reflect"
"strconv"
"strings"
)

type intslice struct {
data *[]int
}

func NewIntSlice(data *[]int) *intslice {
return &intslice{data}
}

func (i *intslice) String() string {
return fmt.Sprintf("%d", *i.data)
}

// The second method is Set(value string) error
func (i *intslice) Set(value string) error {
tmp := []int{}
res := strings.Split(value, ",")
for _, p := range res {
p1 := strings.TrimSpace(p)
val, err := strconv.Atoi(p1)
if err != nil {
return fmt.Errorf("Cannot parse %s as []int\n", value)
}

tmp = append(tmp, val)
}
*i.data = tmp

return nil
}

var intSliceType = reflect.TypeOf([]int{})
34 changes: 34 additions & 0 deletions strslice.go
@@ -0,0 +1,34 @@
package uniconfig

import (
"fmt"
"reflect"
"strings"
)

type strslice struct {
data *[]string
}

func NewStrSlice(data *[]string) *strslice {
return &strslice{data}
}

func (i *strslice) String() string {
return fmt.Sprintf("%+v", *i.data)
}

// The second method is Set(value string) error
func (i *strslice) Set(value string) error {
tmp := []string{}
res := strings.Split(value, ",")
for _, p := range res {
val := strings.TrimSpace(p)
tmp = append(tmp, val)
}
*i.data = tmp

return nil
}

var strSliceType = reflect.TypeOf([]string{})
21 changes: 19 additions & 2 deletions uniconfig.go
Expand Up @@ -47,6 +47,23 @@ func (i *ConfigItem) InitFlag() {
case reflect.Bool:
v := i.Value.Addr().Interface().(*bool)
flag.BoolVar(v, name, *v, i.Help)
case reflect.Slice:
switch i.Value.Type() {
case intSliceType:
v := i.Value.Addr().Interface().(*[]int)
v1 := NewIntSlice(v)
flag.Var(v1, name, i.Help)
case strSliceType:
v := i.Value.Addr().Interface().(*[]string)
v1 := NewStrSlice(v)
flag.Var(v1, name, i.Help)
case floatSliceType:
v := i.Value.Addr().Interface().(*[]float64)
v1 := NewFloatSlice(v)
flag.Var(v1, name, i.Help)
default:
log.Fatalf("Unexpected type of config entry: %v", i)
}
default:
log.Fatalf("Unexpected type of config entry: %v", i)
}
Expand Down Expand Up @@ -141,8 +158,8 @@ func GetConfigPathFromCmd(args []string) string {
// (and therefore flag.Parse() must be called *after* we've read the config file)
reArgValue := regexp.MustCompile(`^[^=]+="?(.+?)"?$`) // optionally quoted value
for i, arg := range args {
if (arg == "-config" || arg == "--config") && i < len(args) - 1 {
return args[i + 1]
if (arg == "-config" || arg == "--config") && i < len(args)-1 {
return args[i+1]
}
if strings.HasPrefix(arg, "-config=") || strings.HasPrefix(arg, "--config=") {
if m := reArgValue.FindStringSubmatch(arg); m != nil {
Expand Down