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

fix assign the correct value to the proxy

parent af461a4e
No related branches found
No related tags found
No related merge requests found
......@@ -57,16 +57,17 @@ type Definition struct {
The following tags are supported:
| Tag | Context | Description |
|---------------|---------|--------------------------------------------|
| `short` | Value | Short name of the flag. |
| `long` | Value | Long name of the flag. |
| `description` | Value | Description of the flag. |
| `required` | Value | Flag is required. |
| `shadow` | Value | Copy the value to the shadow structure. |
| `command` | Command | Flag is a command. |
| `call` | Command | Function to call when the command is used. |
| `ignore` | -/- | Property is ignored. |
| Tag | Context | Description |
|---------------|----------|------------------------------------------------|
| `short` | Value | Short name of the flag. |
| `long` | Value | Long name of the flag. |
| `description` | Value | Description of the flag. |
| `required` | Value | Flag is required. |
| `proxy` | Value | Copy the value to the proxy structure. |
| `command` | Command | Flag is a command. |
| `call` | Command | Function to call when the command is used. |
| `ignore` | -/- | Property is ignored. |
### Callbacks
......@@ -148,9 +149,8 @@ setting.Execute()
### Proxy
The proxy structure is used to copy the values of the flags to the
proxy structure. The proxy structure is used to access the values of the
flags.
The proxy structure is used to copy the values of the flags to a other
structure.
The proxy structure must implement the `Proxy` interface.
......@@ -174,6 +174,8 @@ func main() {
}
```
The path in the structure is defined by the tag `proxy`.
### Arguments
the free arguments can be fetched with the method `Args()`.
......
......@@ -12,6 +12,7 @@ type cmd[C any] struct {
name string
flagSet *flag.FlagSet
tagMapping map[string]string
proxyMapping map[string]string
commands []*cmd[C]
settings *Settings[C]
valuePath []string
......@@ -60,6 +61,7 @@ func buildCommandStruct[C any](s *Settings[C], name, fkt string, errorHandling f
commands: []*cmd[C]{},
settings: s,
tagMapping: map[string]string{},
proxyMapping: map[string]string{},
valuePath: path,
functionName: fkt,
}
......@@ -148,6 +150,10 @@ func (c *cmd[C]) parseStruct(dta any) {
continue
}
if m[tagProxy] != "" {
c.proxyMapping[v.Type().Field(i).Name] = m[tagProxy]
}
if m[tagShort] != "" {
c.tagMapping[m[tagShort]] = v.Type().Field(i).Name
}
......
......@@ -12,12 +12,12 @@ import (
type testExecutionStruct struct {
callbackCounter int `ignore:"true"`
// for tag shadow see TestFlagCopyToShadow
Global1 bool `short:"a" long:"global1" description:"Global 1" shadow:"ValGlobal1"`
// for tag proxy see TestFlagCopyToProxy
Global1 bool `short:"a" long:"global1" description:"Global 1" proxy:"ValGlobal1"`
Global2 bool `short:"b" long:"global2" description:"Global 2"`
Command1 struct {
Command1Flag1 bool `short:"c" long:"command1flag1" description:"Command 1 Flag 1"`
Command1Flag2 bool `short:"d" long:"command1flag2" description:"Command 1 Flag 2" shadow:"ValCommand1Flag2"`
Command1Flag2 bool `short:"d" long:"command1flag2" description:"Command 1 Flag 2" proxy:"ValCommand1Flag2"`
Command2 struct {
Command2Flag1 bool `short:"e" long:"command2flag1" description:"Command 2 Flag 1"`
Command2Flag2 bool `short:"f" long:"command2flag2" description:"Command 2 Flag 2"`
......
......@@ -53,6 +53,10 @@ func (s *Settings[C]) assignValues(c cmd[C]) {
s.errors = append(s.errors, err)
}
if c.proxyMapping[k] != "" {
p = c.proxyMapping[k]
}
s.mapping[p] = value
return
......
......@@ -21,7 +21,7 @@ type ConfigStruct6 struct {
ValSub ConfigStruct6Sub1
}
func TestFlagCopyToShadow(t *testing.T) {
func TestFlagCopyToProxy(t *testing.T) {
c := ConfigStruct6{}
c.ValSub.Command3Flag1 = true
......@@ -40,8 +40,9 @@ func TestFlagCopyToShadow(t *testing.T) {
}
func (s *ConfigStruct6) Copy(m map[string]any) {
pathfinder.SetValue(s, "ValGlobal1", (m["Global1"]))
pathfinder.SetValue(s, "ValCommand1Flag2", (m["Command1.Command1Flag2"]))
for k, v := range m {
pathfinder.SetValue(s, k, v)
}
}
func TestCopyable(t *testing.T) {
......
......@@ -15,6 +15,7 @@ const (
tagShort = "short"
tagLong = "long"
tagDescription = "description"
tagProxy = "proxy"
)
func getTagMap(field reflect.StructField) (value map[string]string) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment