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