Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • oss/libraries/go/application/xflags
1 result
Select Git revision
Show changes
Commits on Source (4)
<a name="v1.13.0"></a>
## [v1.13.0] - 2022-10-23
### Add Features
- feat change proxy to map and expose map
### Changes
- chore add licenses
<a name="v1.12.0"></a>
## [v1.12.0] - 2022-10-19
### Bug Fixes
......@@ -141,6 +150,7 @@
<a name="v1.0.0"></a>
## v1.0.0 - 2022-10-04
[v1.13.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.12.0...v1.13.0
[v1.12.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.11.0...v1.12.0
[v1.11.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.10.2...v1.11.0
[v1.10.2]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.10.1...v1.10.2
......
......@@ -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
......
......@@ -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
}
......@@ -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"])
}
......@@ -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] != "" {
......
......@@ -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"`
......
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package xflags
import (
......
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package xflags
import (
......
......@@ -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)
}
......
......@@ -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"})
......
{"version":"1.12.0"}
{"version":"1.13.0"}
......@@ -31,7 +31,7 @@ type Settings[C any] struct {
config config
mapping map[string]any
proxy Proxy
proxy Copyable
wasExecuted bool
hint string
......
......@@ -15,7 +15,7 @@ const (
tagShort = "short"
tagLong = "long"
tagDescription = "description"
tagProxy = "proxy"
tagMapping = "map"
)
func getTagMap(field reflect.StructField) (value map[string]string) {
......