diff --git a/README.md b/README.md index 5abdada2e5668b95640df4acb9a4510e61804cdb..8cc9af22ce5040cff2799f22567e2c27cc0a8ea5 100644 --- a/README.md +++ b/README.md @@ -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()`. diff --git a/command.go b/command.go index 9ac4106e55fbf8aab5eee74147c32834b48a8b99..37721f9362a0c346ceb33a486143366d4f986d52 100644 --- a/command.go +++ b/command.go @@ -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 } diff --git a/execute_test.go b/execute_test.go index f44dfac59e183d9116b928d397fbaf48492a4419..bb19486fbdec8621b3c6437307d9ad0051d38749 100644 --- a/execute_test.go +++ b/execute_test.go @@ -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"` diff --git a/mapping.go b/mapping.go index f0e8a26b61773f4c1dbf6ec6c4cc573c49bc45f8..57acfd20d40db8262bdecb663f4982f75e37d255 100644 --- a/mapping.go +++ b/mapping.go @@ -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 diff --git a/mapping_test.go b/mapping_test.go index 93a3dc02b52e6adb4ee7b98c999aad4eb49a8ca9..d7698c14daf1015db18839e5549138a0747f6545 100644 --- a/mapping_test.go +++ b/mapping_test.go @@ -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) { diff --git a/tags.go b/tags.go index 1e0dad71e698c0d01b332c6bcc23811765005c20..47fa53879744ba0bd2d295b7d97a53ee406abb33 100644 --- a/tags.go +++ b/tags.go @@ -15,6 +15,7 @@ const ( tagShort = "short" tagLong = "long" tagDescription = "description" + tagProxy = "proxy" ) func getTagMap(field reflect.StructField) (value map[string]string) {