diff --git a/README.md b/README.md index 8cc9af22ce5040cff2799f22567e2c27cc0a8ea5..bb8c233271be85030a3bb3e714094432975d0995 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ It supports: * [X] Define flags in a structure * [X] Define callbacks for flags * [X] Define default values for flags -* [X] Define aliases for flags -* [X] Define required flags +* [X] Define a map for values ## Installation @@ -57,16 +56,16 @@ 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. | -| `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. | +| 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. | +| `map` | Value | Copy the value to the mapped structure. | +| `command` | Command | Flag is a command. | +| `call` | Command | Function to call when the command is used. | +| `ignore` | -/- | Property is ignored. | ### Callbacks @@ -147,12 +146,13 @@ The function `Execute()` executes the command. See the section setting.Execute() ``` -### Proxy +### Mapped Values -The proxy structure is used to copy the values of the flags to a other -structure. +The mapped structure is used to copy the +values of the flags to another structure +and to a map. -The proxy structure must implement the `Proxy` interface. +The mapped structure must implement the `Copyable` interface. ```go type MyObj struct { @@ -168,13 +168,17 @@ func (m *MyObj) Copy(_ map[string]any) { func main() { setting := New(os.Args[0], Definition{}) - setting.SetProxy(&MyObj{}) + setting.SetMappedObject(&MyObj{}) setting.Parse(os.Args[1:]) setting.Execute() } ``` -The path in the structure is defined by the tag `proxy`. +The path in the structure is defined by the tag `map`. + +Die Map der Werte kann über die Methode `GetMap()` abgerufen werden. + + ### Arguments diff --git a/api.go b/api.go index b6fc6ac925b875205ff606d2dc3669e94aacfaef..efdde2664b3f5619a502f0f33c87ed90701a09a6 100644 --- a/api.go +++ b/api.go @@ -17,7 +17,7 @@ type dummyCopyArg struct{} func (n dummyCopyArg) Copy(_ map[string]any) {} // Execute executes the command line arguments and calls the functions. -func Execute[C any](cmd C, cpy ...Proxy) *Settings[C] { +func Execute[C any](cmd C, cpy ...Copyable) *Settings[C] { if cpy == nil { return execute(cmd, dummyCopyArg{}, os.Args[0], os.Args[1:]) @@ -41,14 +41,14 @@ func (s *Settings[C]) GetFlagOutput() { } // execute is the internal implementation of Execute. -func execute[C any, D Proxy](cmd C, proxy D, name string, args []string) *Settings[C] { +func execute[C any, D Copyable](cmd C, proxy D, name string, args []string) *Settings[C] { instance := New(name, cmd) if instance.HasErrors() { return instance } if (reflect.ValueOf(&proxy).Elem().Type() != reflect.TypeOf(dummyCopyArg{})) { - instance.SetProxy(proxy) + instance.SetMappedObject(proxy) if instance.HasErrors() { return instance } @@ -116,3 +116,7 @@ func (s *Settings[C]) GetDefaults() string { s.flagOutput = mem return r } + +func (s *Settings[C]) GetMap() map[string]any { + return s.mapping +} diff --git a/api_test.go b/api_test.go index fa7e9259423a46245b333cb887a155f5f59ee49d..8bec1094500588fce04799fea104f96aad571acc 100644 --- a/api_test.go +++ b/api_test.go @@ -102,3 +102,21 @@ func TestCommand2(t *testing.T) { assert.False(t, commands.HasErrors()) } + +func TestProxyMapping(t *testing.T) { + + commands := New("root", testExecutionStruct{}) + args := []string{"-a", "command1", "-d"} + + commands.Parse(args) + + assert.False(t, commands.HasErrors()) + if commands.HasErrors() { + t.Log(commands.Errors()) + } + + m := commands.GetMap() + assert.Equal(t, "true", m["ValGlobal1"]) + assert.Equal(t, "true", m["ValCommand1Flag2"]) + +} diff --git a/command.go b/command.go index eccb273808a880859a35c9d138e63dd8f0197504..c5b45545da7af9d0028c8f9b9c8cde00728ad5b9 100644 --- a/command.go +++ b/command.go @@ -150,8 +150,8 @@ func (c *cmd[C]) parseStruct(dta any) { continue } - if m[tagProxy] != "" { - c.proxyMapping[v.Type().Field(i).Name] = m[tagProxy] + if m[tagMapping] != "" { + c.proxyMapping[v.Type().Field(i).Name] = m[tagMapping] } if m[tagShort] != "" { diff --git a/execute_test.go b/execute_test.go index bb19486fbdec8621b3c6437307d9ad0051d38749..5275ca7051edf0128dc22446b6acfe62a5e80b0c 100644 --- a/execute_test.go +++ b/execute_test.go @@ -13,11 +13,11 @@ type testExecutionStruct struct { callbackCounter int `ignore:"true"` // for tag proxy see TestFlagCopyToProxy - Global1 bool `short:"a" long:"global1" description:"Global 1" proxy:"ValGlobal1"` + Global1 bool `short:"a" long:"global1" description:"Global 1" map:"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" proxy:"ValCommand1Flag2"` + Command1Flag2 bool `short:"d" long:"command1flag2" description:"Command 1 Flag 2" map:"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 dd1e3de65dda45f5cd8ca3ce65a8acae2912b6e9..7531bb5bc7eeee14d6e5ec6f70230c8fd5549674 100644 --- a/mapping.go +++ b/mapping.go @@ -10,8 +10,8 @@ import ( "strings" ) -// SetProxy sets the shadow struct for the flag configuration. -func (s *Settings[C]) SetProxy(proxy Proxy) *Settings[C] { +// SetMappedObject sets the shadow struct for the flag configuration. +func (s *Settings[C]) SetMappedObject(proxy Copyable) *Settings[C] { if reflect.TypeOf(proxy).Kind() != reflect.Ptr { s.errors = append(s.errors, ShadowMustBePointerError) @@ -27,8 +27,8 @@ func (s *Settings[C]) SetProxy(proxy Proxy) *Settings[C] { return s } -// Proxy is the interface for the proxy struct. -type Proxy interface { +// Copyable is the interface for the proxy struct. +type Copyable interface { Copy(map[string]any) } diff --git a/mapping_test.go b/mapping_test.go index d7698c14daf1015db18839e5549138a0747f6545..46a712aed71dbe108b0b28ab92753f2ab443b7f9 100644 --- a/mapping_test.go +++ b/mapping_test.go @@ -29,7 +29,7 @@ func TestFlagCopyToProxy(t *testing.T) { settings := New("test", testExecutionStruct{}) assert.NotNil(t, settings) - settings.SetProxy(&c) + settings.SetMappedObject(&c) assert.False(t, settings.HasErrors()) settings.Parse([]string{"-a", "command1", "-d"}) diff --git a/setting.go b/setting.go index f4456a34f6c1c51ae074afc846e1880eafdb8cda..78e1acd9699969f52785296e4032a78536fccacb 100644 --- a/setting.go +++ b/setting.go @@ -31,7 +31,7 @@ type Settings[C any] struct { config config mapping map[string]any - proxy Proxy + proxy Copyable wasExecuted bool hint string diff --git a/tags.go b/tags.go index 47fa53879744ba0bd2d295b7d97a53ee406abb33..f019823b57e9b7374e63e1cfeb7c3ba5792bec3d 100644 --- a/tags.go +++ b/tags.go @@ -15,7 +15,7 @@ const ( tagShort = "short" tagLong = "long" tagDescription = "description" - tagProxy = "proxy" + tagMapping = "map" ) func getTagMap(field reflect.StructField) (value map[string]string) {