Skip to content
Snippets Groups Projects
Verified Commit 2b16160e authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: support multiple flags

parent 20c6561b
No related branches found
No related tags found
No related merge requests found
......@@ -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] != "" {
......
......@@ -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)
}
......
type.go 0 → 100644
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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment