From 2b16160ef6fb5e1977dfb2dd57ec80fbb931bdaa Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Fri, 23 Dec 2022 10:16:30 +0100 Subject: [PATCH] feat: support multiple flags --- command.go | 16 +++++++++++++++- hint.go | 0 mapping.go | 13 ++++++++++++- type.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 hint.go create mode 100644 type.go diff --git a/command.go b/command.go index c5b4554..e58b438 100644 --- a/command.go +++ b/command.go @@ -122,6 +122,21 @@ func (c *cmd[C]) initFlags(x reflect.Value, m map[string]string) { if m[tagLong] != "" { c.flagSet.String(m[tagLong], x.String(), m[tagDescription]) } + case reflect.Slice: + + if x.Type() == reflect.TypeOf(StringFlags{}) { + + if m[tagShort] != "" { + xx := x.Interface().(StringFlags) + c.flagSet.Var(&xx, m[tagShort], m[tagDescription]) + } + + if m[tagLong] != "" { + xx := x.Interface().(StringFlags) + c.flagSet.Var(&xx, m[tagLong], m[tagDescription]) + } + + } default: c.settings.errors = append(c.settings.errors, newUnsupportedFlagTypeError(x.Type())) @@ -163,7 +178,6 @@ func (c *cmd[C]) parseStruct(dta any) { } c.initFlags(x, m) } else if m[tagCommand] != "" { - //c.tagMapping["cmd"+m[tagCommand]] = v.Type().Field(i).Name c.initCommands(x, m, v.Type().Field(i).Name) } else if m[tagIgnore] != "" { diff --git a/hint.go b/hint.go new file mode 100644 index 0000000..e69de29 diff --git a/mapping.go b/mapping.go index 7531bb5..19a0b66 100644 --- a/mapping.go +++ b/mapping.go @@ -48,7 +48,18 @@ func (s *Settings[C]) assignValues(c cmd[C]) { pa := append(c.valuePath, k) p := strings.Join(pa, ".") - err := pathfinder.SetValue(&s.definitions, p, value) + q, err := pathfinder.GetValue(&s.definitions, p) + if err != nil { + s.errors = append(s.errors, err) + return + } + + if q == nil { + q = reflect.New(reflect.TypeOf(q).Elem()).Interface() + } + + err = pathfinder.SetValue(&s.definitions, p, value) + if err != nil { s.errors = append(s.errors, err) } diff --git a/type.go b/type.go new file mode 100644 index 0000000..48172c4 --- /dev/null +++ b/type.go @@ -0,0 +1,30 @@ +package xflags + +import "strings" + +type StringFlags []string + +func (i *StringFlags) String() string { + return strings.Join(*i, ",") +} + +func (i *StringFlags) Set(value string) error { + *i = append(*i, value) + return nil +} + +type IntFlags []int + +func (i *IntFlags) String() string { + r := make([]string, len(*i)) + for k, v := range *i { + r[k] = string(v) + } + + return strings.Join(r, ",") +} + +func (i *IntFlags) Set(value int) error { + *i = append(*i, value) + return nil +} -- GitLab