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 (5)
<a name="v1.2.0"></a>
## [v1.2.0] - 2022-10-05
### Add Features
- feat new function ParseOsArgs
### Bug Fixes
- fix Settings should be exported
- fix parse only the arguments and not the programm
<a name="v1.1.1"></a> <a name="v1.1.1"></a>
## [v1.1.1] - 2022-10-05 ## [v1.1.1] - 2022-10-05
### Bug Fixes ### Bug Fixes
...@@ -11,5 +21,6 @@ ...@@ -11,5 +21,6 @@
<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.2.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.1.1...v1.2.0
[v1.1.1]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.1.0...v1.1.1 [v1.1.1]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.1.0...v1.1.1
[v1.1.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.0.0...v1.1.0 [v1.1.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.0.0...v1.1.0
...@@ -10,9 +10,9 @@ import ( ...@@ -10,9 +10,9 @@ import (
// New creates a new instance of the settings. // New creates a new instance of the settings.
// name should be the name of the command and comes from the first argument of the command line. // name should be the name of the command and comes from the first argument of the command line.
// os.Args[0] is a good choice. // os.Args[0] is a good choice.
func New[C any](name string, definitions C) *setting[C] { func New[C any](name string, definitions C) *Settings[C] {
s := &setting[C]{ s := &Settings[C]{
config: config{ config: config{
errorHandling: flag.ContinueOnError, errorHandling: flag.ContinueOnError,
}, },
...@@ -32,32 +32,11 @@ func New[C any](name string, definitions C) *setting[C] { ...@@ -32,32 +32,11 @@ func New[C any](name string, definitions C) *setting[C] {
} }
// FlagOutput returns the writer where the flag package writes its output. // FlagOutput returns the writer where the flag package writes its output.
func (s *setting[C]) FlagOutput() string { func (s *Settings[C]) FlagOutput() string {
return s.flagOutput.(*bytes.Buffer).String() return s.flagOutput.(*bytes.Buffer).String()
} }
// Args Returns not parsed arguments. // Args Returns not parsed arguments.
func (s *setting[C]) Args() []string { func (s *Settings[C]) Args() []string {
return s.args return s.args
} }
//type ConfigAny[C any] struct {
// path string
// ConfigurationAdapter[C]
//}
//
//type ConfigurationAdapter[RT any] interface {
// InitFromFlagSet(flagSet *flag.FlagSet) RT
//}
//func (s *setting[C]) CopyValuesToStruct(path string, adapter any) *setting[C] {
//
// if s.command == nil {
// s.errors = append(s.errors, MissingCommandError)
// return s
// }
//
// s.command.copyValuesToStruct(path, adapter)
//
// return s
//}
...@@ -43,7 +43,7 @@ type CmdTest1 struct { ...@@ -43,7 +43,7 @@ type CmdTest1 struct {
func TestCommand2(t *testing.T) { func TestCommand2(t *testing.T) {
commands := New("root", CmdTest1{}) commands := New("root", CmdTest1{})
args := []string{"root", "-a", "sub1", "-b", "sub2", "-c", "sub3", "-d"} args := []string{"-a", "sub1", "-b", "sub2", "-c", "sub3", "-d"}
commands.Parse(args) commands.Parse(args)
......
...@@ -11,7 +11,7 @@ type cmd[C any] struct { ...@@ -11,7 +11,7 @@ type cmd[C any] struct {
tagMapping map[string]string tagMapping map[string]string
shadowMapping map[string]string shadowMapping map[string]string
commands []*cmd[C] commands []*cmd[C]
settings *setting[C] settings *Settings[C]
valuePath []string valuePath []string
functionName string functionName string
} }
...@@ -51,7 +51,7 @@ func (c *cmd[C]) parse(args []string) { ...@@ -51,7 +51,7 @@ func (c *cmd[C]) parse(args []string) {
} }
func buildCommandStruct[C any](s *setting[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] { func buildCommandStruct[C any](s *Settings[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] {
cc := &cmd[C]{ cc := &cmd[C]{
name: name, name: name,
flagSet: flag.NewFlagSet(name, errorHandling), flagSet: flag.NewFlagSet(name, errorHandling),
......
...@@ -30,7 +30,7 @@ func TestParse(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestParse(t *testing.T) {
t.Run(test.commandline[0], func(t *testing.T) { t.Run(test.commandline[0], func(t *testing.T) {
c := &cmd[testStructParseCommand1]{} c := &cmd[testStructParseCommand1]{}
c.settings = &setting[testStructParseCommand1]{} c.settings = &Settings[testStructParseCommand1]{}
c.commands = []*cmd[testStructParseCommand1]{} c.commands = []*cmd[testStructParseCommand1]{}
......
...@@ -7,18 +7,18 @@ import ( ...@@ -7,18 +7,18 @@ import (
// ResetError is used to reset the error to nil // ResetError is used to reset the error to nil
// After calling this function, the call HasErrors() will return false // After calling this function, the call HasErrors() will return false
func (s *setting[C]) ResetErrors() *setting[C] { func (s *Settings[C]) ResetErrors() *Settings[C] {
s.errors = []error{} s.errors = []error{}
return s return s
} }
// Check if the setting contains errors // Check if the setting contains errors
func (s *setting[C]) HasErrors() bool { func (s *Settings[C]) HasErrors() bool {
return len(s.errors) > 0 return len(s.errors) > 0
} }
// Get all errors // Get all errors
func (s *setting[C]) Errors() []error { func (s *Settings[C]) Errors() []error {
return s.errors return s.errors
} }
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"reflect" "reflect"
) )
func (s *setting[C]) Execute() *setting[C] { func (s *Settings[C]) Execute() *Settings[C] {
if len(s.errors) > 0 { if len(s.errors) > 0 {
return s return s
} }
......
...@@ -25,15 +25,15 @@ type testExecutionStruct struct { ...@@ -25,15 +25,15 @@ type testExecutionStruct struct {
} `command:"command3" description:"Command 3" call:"Command3Callback" ` } `command:"command3" description:"Command 3" call:"Command3Callback" `
} }
func (c *testExecutionStruct) Command1Callback(s *setting[testExecutionStruct]) { func (c *testExecutionStruct) Command1Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++ s.definitions.callbackCounter++
} }
func (c *testExecutionStruct) Command2Callback(s *setting[testExecutionStruct]) { func (c *testExecutionStruct) Command2Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++ s.definitions.callbackCounter++
} }
func (c *testExecutionStruct) Command3Callback(s *setting[testExecutionStruct]) { func (c *testExecutionStruct) Command3Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++ s.definitions.callbackCounter++
} }
...@@ -48,16 +48,16 @@ func TestExec(t *testing.T) { ...@@ -48,16 +48,16 @@ func TestExec(t *testing.T) {
tests := []testExecutionTestCases[testExecutionStruct]{ tests := []testExecutionTestCases[testExecutionStruct]{
{ {
name: "test", name: "test",
args: []string{"test", "command1", "-c"}, args: []string{"command1", "-c"},
targetValue: 1, targetValue: 1,
}, },
{ {
name: "test", name: "test",
args: []string{"test", "command1", "command2", "-e"}, args: []string{"command1", "command2", "-e"},
targetValue: 2, targetValue: 2,
}, { }, {
name: "test", name: "test",
args: []string{"test", "-a", "command3"}, args: []string{"-a", "command3"},
targetValue: 1, targetValue: 1,
}, },
} }
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"strings" "strings"
) )
func (s *setting[C]) assignValues(c cmd[C]) { func (s *Settings[C]) assignValues(c cmd[C]) {
flgs := c.flagSet flgs := c.flagSet
flgs.Visit(func(f *flag.Flag) { flgs.Visit(func(f *flag.Flag) {
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"testing" "testing"
) )
type testInterfaceCallbacks func(s *setting[testIntegrationStruct]) type testInterfaceCallbacks func(s *Settings[testIntegrationStruct])
type testIntegrationStruct struct { type testIntegrationStruct struct {
Help bool `short:"h" long:"help" description:"Show this help message"` Help bool `short:"h" long:"help" description:"Show this help message"`
...@@ -24,7 +24,7 @@ func TestIntegrationError(t *testing.T) { ...@@ -24,7 +24,7 @@ func TestIntegrationError(t *testing.T) {
tests := []testIntegrationTestCases[testIntegrationStruct]{ tests := []testIntegrationTestCases[testIntegrationStruct]{
{ {
name: "test", name: "test",
args: []string{"test2", "-a"}, args: []string{"-a"},
errors: []string{ errors: []string{
"flag provided but not defined: -a", "flag provided but not defined: -a",
}, },
...@@ -60,8 +60,8 @@ func TestIntegration(t *testing.T) { ...@@ -60,8 +60,8 @@ func TestIntegration(t *testing.T) {
tests := []testIntegrationTestCases[testIntegrationStruct]{ tests := []testIntegrationTestCases[testIntegrationStruct]{
{ {
name: "test", name: "test",
args: []string{"test"}, args: []string{},
test: func(s *setting[testIntegrationStruct]) { test: func(s *Settings[testIntegrationStruct]) {
v := s.GetValues() v := s.GetValues()
assert.NotNil(t, v) assert.NotNil(t, v)
assert.IsType(t, testIntegrationStruct{}, v) assert.IsType(t, testIntegrationStruct{}, v)
...@@ -71,8 +71,8 @@ func TestIntegration(t *testing.T) { ...@@ -71,8 +71,8 @@ func TestIntegration(t *testing.T) {
}, },
{ {
name: "test", name: "test",
args: []string{"test", "-v"}, args: []string{"-v"},
test: func(s *setting[testIntegrationStruct]) { test: func(s *Settings[testIntegrationStruct]) {
v := s.GetValues() v := s.GetValues()
assert.NotNil(t, v) assert.NotNil(t, v)
assert.IsType(t, testIntegrationStruct{}, v) assert.IsType(t, testIntegrationStruct{}, v)
...@@ -82,29 +82,29 @@ func TestIntegration(t *testing.T) { ...@@ -82,29 +82,29 @@ func TestIntegration(t *testing.T) {
}, },
{ {
name: "test", name: "test",
args: []string{"test", "--verbose"}, args: []string{"--verbose"},
test: func(s *setting[testIntegrationStruct]) { test: func(s *Settings[testIntegrationStruct]) {
assert.True(t, s.GetValues().Verbose) assert.True(t, s.GetValues().Verbose)
}, },
}, },
{ {
name: "test", name: "test",
args: []string{"test", "-verbose"}, args: []string{"-verbose"},
test: func(s *setting[testIntegrationStruct]) { test: func(s *Settings[testIntegrationStruct]) {
assert.True(t, s.GetValues().Verbose) assert.True(t, s.GetValues().Verbose)
}, },
}, },
{ {
name: "test", name: "test",
args: []string{"test", "sub1"}, args: []string{"sub1"},
test: func(s *setting[testIntegrationStruct]) { test: func(s *Settings[testIntegrationStruct]) {
assert.False(t, s.GetValues().Verbose) assert.False(t, s.GetValues().Verbose)
}, },
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.args[0], func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
settings := New(tt.name, testIntegrationStruct{}) settings := New(tt.name, testIntegrationStruct{})
assert.NotNil(t, settings) assert.NotNil(t, settings)
......
package xflags package xflags
import "os"
// ParseOsArgs parses the os.Args.
func (s *Settings[C]) ParseOsArgs() *Settings[C] {
return s.Parse(os.Args[1:])
}
// Parse parses the command line arguments and assigns the values to the settings. // Parse parses the command line arguments and assigns the values to the settings.
func (s *setting[C]) Parse(args []string) *setting[C] { func (s *Settings[C]) Parse(args []string) *Settings[C] {
if len(s.errors) > 0 { if len(s.errors) > 0 {
return s return s
} }
...@@ -11,7 +18,7 @@ func (s *setting[C]) Parse(args []string) *setting[C] { ...@@ -11,7 +18,7 @@ func (s *setting[C]) Parse(args []string) *setting[C] {
return s return s
} }
err := s.command.flagSet.Parse(args[1:]) err := s.command.flagSet.Parse(args)
if err != nil { if err != nil {
s.errors = append(s.errors, err) s.errors = append(s.errors, err)
return s return s
......
...@@ -13,13 +13,13 @@ type Definition struct { ...@@ -13,13 +13,13 @@ type Definition struct {
} `command:"serve" description:"Run the HTTP server" call:"DoServe"` } `command:"serve" description:"Run the HTTP server" call:"DoServe"`
} }
func (d *Definition) DoServe(_ *setting[Definition]) { func (d *Definition) DoServe(_ *Settings[Definition]) {
// do something // do something
} }
func TestReadMeInit(t *testing.T) { func TestReadMeInit(t *testing.T) {
setting := New("test", Definition{}) setting := New("test", Definition{})
setting.Parse([]string{"test", "-v", "serve", "-h", "localhost", "-p", "8080"}) setting.Parse([]string{"-v", "serve", "-h", "localhost", "-p", "8080"})
setting.Execute() setting.Execute()
assert.True(t, setting.definitions.Verbose) assert.True(t, setting.definitions.Verbose)
assert.False(t, setting.HasErrors()) assert.False(t, setting.HasErrors())
......
{"version":"1.1.1"} {"version":"1.2.0"}
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"io" "io"
) )
func (s *setting[C]) initCommands(name string) { func (s *Settings[C]) initCommands(name string) {
s.command = buildCommandStruct[C](s, name, "", s.config.errorHandling, []string{}) s.command = buildCommandStruct[C](s, name, "", s.config.errorHandling, []string{})
s.command.parseStruct(s.definitions) s.command.parseStruct(s.definitions)
} }
...@@ -14,7 +14,7 @@ type config struct { ...@@ -14,7 +14,7 @@ type config struct {
errorHandling flag.ErrorHandling errorHandling flag.ErrorHandling
} }
type setting[C any] struct { type Settings[C any] struct {
definitions C definitions C
command *cmd[C] command *cmd[C]
...@@ -29,6 +29,6 @@ type setting[C any] struct { ...@@ -29,6 +29,6 @@ type setting[C any] struct {
shadow any shadow any
} }
func (s *setting[C]) GetValues() C { func (s *Settings[C]) GetValues() C {
return s.definitions return s.definitions
} }
...@@ -6,14 +6,14 @@ import ( ...@@ -6,14 +6,14 @@ import (
) )
func TestGetValues(t *testing.T) { func TestGetValues(t *testing.T) {
s := &setting[CmdTest1]{ s := &Settings[CmdTest1]{
definitions: CmdTest1{}, definitions: CmdTest1{},
} }
assert.Equal(t, CmdTest1{}, s.GetValues()) assert.Equal(t, CmdTest1{}, s.GetValues())
} }
func TestInitCommands(t *testing.T) { func TestInitCommands(t *testing.T) {
s := &setting[CmdTest1]{ s := &Settings[CmdTest1]{
definitions: CmdTest1{}, definitions: CmdTest1{},
} }
......
...@@ -3,7 +3,7 @@ package xflags ...@@ -3,7 +3,7 @@ package xflags
import "reflect" import "reflect"
// SetShadow sets the shadow struct for the flag configuration. // SetShadow sets the shadow struct for the flag configuration.
func (s *setting[C]) SetShadow(shadow any) *setting[C] { func (s *Settings[C]) SetShadow(shadow any) *Settings[C] {
if reflect.TypeOf(shadow).Kind() != reflect.Ptr { if reflect.TypeOf(shadow).Kind() != reflect.Ptr {
s.errors = append(s.errors, ShadowMustBePointerError) s.errors = append(s.errors, ShadowMustBePointerError)
......
...@@ -35,7 +35,7 @@ func TestFlagCopyToShadow(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestFlagCopyToShadow(t *testing.T) {
settings.SetShadow(&c) settings.SetShadow(&c)
assert.False(t, settings.HasErrors()) assert.False(t, settings.HasErrors())
settings.Parse([]string{"test", "-a", "command1", "-d"}) settings.Parse([]string{"-a", "command1", "-d"})
assert.True(t, c.ValGlobal1) assert.True(t, c.ValGlobal1)
assert.True(t, c.ValCommand1Flag2) assert.True(t, c.ValCommand1Flag2)
......