Skip to content

Commit

Permalink
support for []int []string []float32 arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
e-max committed Dec 28, 2014
1 parent d7d45d3 commit e78f811
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 14 deletions.
19 changes: 7 additions & 12 deletions example_app/example.go
Expand Up @@ -2,24 +2,19 @@ package main

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

type MyConfig struct {
Debug bool
Count int `help:"number of items"`
Nested1 struct {
A string
B string
ignored string
}
Nested2 struct {
Zzz bool
}
Debug bool
Count int `help:"number of items"`
Intlist []int
Strlist []string
Floatlist []float64
}

func main() {
config := &MyConfig{}
config := &MyConfig{Intlist: []int{1, 2, 3}}
config.Count = 42
uniconfig.Load(config)
log.Printf("Hi")
Expand Down
50 changes: 50 additions & 0 deletions floatslice.go
@@ -0,0 +1,50 @@
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{})

//func (i *intslice) Set(value string) error {
//tmp, err := strconv.Atoi(value)
//if err != nil {
//*i.data = append(*i.data, -1)
//} else {
//*i.data = append(*i.data, tmp)
//}
//return nil
//}
50 changes: 50 additions & 0 deletions intslice.go
@@ -0,0 +1,50 @@
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{})

//func (i *intslice) Set(value string) error {
//tmp, err := strconv.Atoi(value)
//if err != nil {
//*i.data = append(*i.data, -1)
//} else {
//*i.data = append(*i.data, tmp)
//}
//return nil
//}
44 changes: 44 additions & 0 deletions strslice.go
@@ -0,0 +1,44 @@
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{})

//func (i *intslice) Set(value string) error {
//tmp, err := strconv.Atoi(value)
//if err != nil {
//*i.data = append(*i.data, -1)
//} else {
//*i.data = append(*i.data, tmp)
//}
//return nil
//}
27 changes: 25 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 @@ -82,6 +99,12 @@ func ScanConfig(config interface{}) []*ConfigItem {
}
continue
}
//if f.Type.Kind() == reflect.Slice {
//z := reflect.SliceOf(f.Type)
//if z.Kind() == reflect.Int {
//}

//}
item := &ConfigItem{
Section: "",
Name: f.Name,
Expand Down Expand Up @@ -141,8 +164,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

0 comments on commit e78f811

Please sign in to comment.