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
  • master
  • v1.0.0
  • v1.1.0
  • v1.1.1
  • v1.10.0
  • v1.10.1
  • v1.10.2
  • v1.11.0
  • v1.12.0
  • v1.13.0
  • v1.13.1
  • v1.13.2
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.16.1
  • v1.16.2
  • v1.16.3
  • v1.16.4
  • v1.16.5
  • v1.2.0
  • v1.2.1
  • v1.2.2
  • v1.2.3
  • v1.3.0
  • v1.3.1
  • v1.4.0
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.8.0
  • v1.8.1
  • v1.8.2
  • v1.8.3
  • v1.9.0
35 results
Show changes
Commits on Source (4)
<a name="v1.7.0"></a>
## [v1.7.0] - 2022-10-14
### Add Features
- feat introduction of copy-interface
- feat new method to get the output of the flags
<a name="v1.6.0"></a> <a name="v1.6.0"></a>
## [v1.6.0] - 2022-10-13 ## [v1.6.0] - 2022-10-13
### Code Refactoring ### Code Refactoring
...@@ -74,6 +81,7 @@ ...@@ -74,6 +81,7 @@
<a name="v1.0.0"></a> <a name="v1.0.0"></a>
## v1.0.0 - 2022-10-04 ## v1.0.0 - 2022-10-04
[v1.7.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.6.0...v1.7.0
[v1.6.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.5.0...v1.6.0 [v1.6.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.5.0...v1.6.0
[v1.5.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.4.0...v1.5.0 [v1.5.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.4.0...v1.5.0
[v1.4.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.3.1...v1.4.0 [v1.4.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.3.1...v1.4.0
......
...@@ -13,23 +13,31 @@ import ( ...@@ -13,23 +13,31 @@ import (
) )
// ExecuteWithShadow executes the command line arguments and calls the functions. // ExecuteWithShadow executes the command line arguments and calls the functions.
func ExecuteWithShadow[C any, D any](cmd C, cnf D) *Settings[C] { func ExecuteWithShadow[C any, D Copyable[D]](cmd C, cnf D) *Settings[C] {
return execute(cmd, cnf, os.Args[0], os.Args[1:]) return execute(cmd, cnf, os.Args[0], os.Args[1:])
} }
type noShadow struct{} type noShadow struct{}
func (n noShadow) Copy(a noShadow) {}
// Execute executes the command line arguments and calls the functions. // Execute executes the command line arguments and calls the functions.
func Execute[C any](cmd C) *Settings[C] { func Execute[C any](cmd C) *Settings[C] {
return execute(cmd, noShadow{}, os.Args[0], os.Args[1:]) return execute(cmd, noShadow{}, os.Args[0], os.Args[1:])
} }
// PrintFlagOutput prints the flag output to the standard output.
func (s *Settings[C]) PrintFlagOutput() { func (s *Settings[C]) PrintFlagOutput() {
fmt.Println(s.command.flagSet.Output()) fmt.Println(s.command.flagSet.Output())
} }
// GetFlagOutput returns the flag output.
func (s *Settings[C]) GetFlagOutput() {
fmt.Println(s.command.flagSet.Output())
}
// execute is the internal implementation of ExecuteWithShadow. // execute is the internal implementation of ExecuteWithShadow.
func execute[C any, D any](cmd C, cnf D, name string, args []string) *Settings[C] { func execute[C any, D Copyable[D]](cmd C, cnf D, name string, args []string) *Settings[C] {
instance := New(name, cmd) instance := New(name, cmd)
if instance.HasErrors() { if instance.HasErrors() {
return instance return instance
...@@ -56,6 +64,10 @@ func execute[C any, D any](cmd C, cnf D, name string, args []string) *Settings[C ...@@ -56,6 +64,10 @@ func execute[C any, D any](cmd C, cnf D, name string, args []string) *Settings[C
return instance return instance
} }
if instance.shadow != nil {
cnf.Copy(instance.shadow.(D))
}
instance.Execute() instance.Execute()
if instance.HasErrors() { if instance.HasErrors() {
return instance return instance
......
...@@ -21,22 +21,29 @@ func TestUsage(t *testing.T) { ...@@ -21,22 +21,29 @@ func TestUsage(t *testing.T) {
assert.Equal(t, " -a\tMessage A\n -x int\n \tMessage X\n", usage) assert.Equal(t, " -a\tMessage A\n -x int\n \tMessage X\n", usage)
} }
type TestDataStruct struct {
}
func (s *TestDataStruct) Copy(x *TestDataStruct) {
}
func TestExecuteTypeStringIsNotSupported(t *testing.T) { func TestExecuteTypeStringIsNotSupported(t *testing.T) {
instance := execute("root", CmdTest1{}, "test", []string{"-a", "hello", "-x", "1"})
instance := execute(CmdTest1{}, &TestDataStruct{}, "test", []string{"-a", "hello", "-x", "1"})
err := instance.Errors() err := instance.Errors()
assert.True(t, instance.HasErrors()) assert.True(t, instance.HasErrors())
assert.Equal(t, 1, len(err)) assert.Equal(t, 1, len(err))
e := err[0] e := err[0]
assert.Equal(t, "type string is not supported", e.Error()) assert.Equal(t, "missing command", e.Error())
} }
func TestExecuteHelp(t *testing.T) { func TestExecuteHelp(t *testing.T) {
cnf := struct { instance := execute(CmdTest1{}, &TestDataStruct{}, "test", []string{"-h"})
}{}
instance := execute(CmdTest1{}, &cnf, "test", []string{"-h"})
assert.False(t, instance.HasErrors()) assert.False(t, instance.HasErrors())
} }
......
...@@ -2,55 +2,3 @@ ...@@ -2,55 +2,3 @@
// SPDX-License-Identifier: AGPL-3.0 // SPDX-License-Identifier: AGPL-3.0
package xflags package xflags
import (
"flag"
"strings"
)
func (s *Settings[C]) assignValues(c cmd[C]) {
flgs := c.flagSet
flgs.Visit(func(f *flag.Flag) {
name := f.Name
value := f.Value.String()
k, ok := c.tagMapping[name]
if !ok {
s.errors = append(s.errors, newUnknownFlagError(name))
return
}
pa := append(c.valuePath, k)
p := strings.Join(pa, ".")
err := setValueUsingPath(&s.definitions, p, value)
if err != nil {
s.errors = append(s.errors, err)
}
err = c.setShadowValue(s.shadow, k, value)
if err != nil {
s.errors = append(s.errors, err)
}
return
})
}
func (c cmd[C]) setShadowValue(obj any, k string, value string) error {
if obj == nil {
return nil
}
// set shadow
n, ok := c.shadowMapping[k]
if !ok {
return nil
}
return setValueUsingPath(obj, n, value)
}
{"version":"1.6.0"} {"version":"1.7.0"}
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
package xflags package xflags
import "reflect" import (
"flag"
"reflect"
"strings"
)
// SetShadow sets the shadow struct for the flag configuration. // SetShadow sets the shadow struct for the flag configuration.
func (s *Settings[C]) SetShadow(shadow any) *Settings[C] { func (s *Settings[C]) SetShadow(shadow any) *Settings[C] {
...@@ -21,3 +25,54 @@ func (s *Settings[C]) SetShadow(shadow any) *Settings[C] { ...@@ -21,3 +25,54 @@ func (s *Settings[C]) SetShadow(shadow any) *Settings[C] {
s.shadow = shadow s.shadow = shadow
return s return s
} }
type Copyable[C any] interface {
Copy(data C)
}
func (s *Settings[C]) assignValues(c cmd[C]) {
flgs := c.flagSet
flgs.Visit(func(f *flag.Flag) {
name := f.Name
value := f.Value.String()
k, ok := c.tagMapping[name]
if !ok {
s.errors = append(s.errors, newUnknownFlagError(name))
return
}
pa := append(c.valuePath, k)
p := strings.Join(pa, ".")
err := setValueUsingPath(&s.definitions, p, value)
if err != nil {
s.errors = append(s.errors, err)
}
err = c.setShadowValue(s.shadow, k, value)
if err != nil {
s.errors = append(s.errors, err)
}
return
})
}
func (c cmd[C]) setShadowValue(obj any, k string, value string) error {
if obj == nil {
return nil
}
// set shadow
n, ok := c.shadowMapping[k]
if !ok {
return nil
}
return setValueUsingPath(obj, n, value)
}
...@@ -44,3 +44,17 @@ func TestFlagCopyToShadow(t *testing.T) { ...@@ -44,3 +44,17 @@ func TestFlagCopyToShadow(t *testing.T) {
assert.True(t, c.ValCommand1Flag2) assert.True(t, c.ValCommand1Flag2)
} }
func (s *ConfigStruct6) Copy(p *ConfigStruct6) {
}
func TestCopyable(t *testing.T) {
c := ConfigStruct6{}
c.ValSub.Command3Flag1 = true
settings := execute(testExecutionStruct{}, &c, "test", []string{"-a", "command3"})
assert.False(t, settings.HasErrors())
}