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
  • 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

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 (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>
## [v1.1.1] - 2022-10-05
### Bug Fixes
......@@ -11,5 +21,6 @@
<a name="v1.0.0"></a>
## 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.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.0.0...v1.1.0
......@@ -10,9 +10,9 @@ import (
// 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.
// 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{
errorHandling: flag.ContinueOnError,
},
......@@ -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.
func (s *setting[C]) FlagOutput() string {
func (s *Settings[C]) FlagOutput() string {
return s.flagOutput.(*bytes.Buffer).String()
}
// Args Returns not parsed arguments.
func (s *setting[C]) Args() []string {
func (s *Settings[C]) Args() []string {
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 {
func TestCommand2(t *testing.T) {
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)
......
......@@ -11,7 +11,7 @@ type cmd[C any] struct {
tagMapping map[string]string
shadowMapping map[string]string
commands []*cmd[C]
settings *setting[C]
settings *Settings[C]
valuePath []string
functionName 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]{
name: name,
flagSet: flag.NewFlagSet(name, errorHandling),
......
......@@ -30,7 +30,7 @@ func TestParse(t *testing.T) {
t.Run(test.commandline[0], func(t *testing.T) {
c := &cmd[testStructParseCommand1]{}
c.settings = &setting[testStructParseCommand1]{}
c.settings = &Settings[testStructParseCommand1]{}
c.commands = []*cmd[testStructParseCommand1]{}
......
......@@ -7,18 +7,18 @@ import (
// ResetError is used to reset the error to nil
// 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{}
return s
}
// Check if the setting contains errors
func (s *setting[C]) HasErrors() bool {
func (s *Settings[C]) HasErrors() bool {
return len(s.errors) > 0
}
// Get all errors
func (s *setting[C]) Errors() []error {
func (s *Settings[C]) Errors() []error {
return s.errors
}
......
......@@ -4,7 +4,7 @@ import (
"reflect"
)
func (s *setting[C]) Execute() *setting[C] {
func (s *Settings[C]) Execute() *Settings[C] {
if len(s.errors) > 0 {
return s
}
......
......@@ -25,15 +25,15 @@ type testExecutionStruct struct {
} `command:"command3" description:"Command 3" call:"Command3Callback" `
}
func (c *testExecutionStruct) Command1Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command1Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
func (c *testExecutionStruct) Command2Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command2Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
func (c *testExecutionStruct) Command3Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command3Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
......@@ -48,16 +48,16 @@ func TestExec(t *testing.T) {
tests := []testExecutionTestCases[testExecutionStruct]{
{
name: "test",
args: []string{"test", "command1", "-c"},
args: []string{"command1", "-c"},
targetValue: 1,
},
{
name: "test",
args: []string{"test", "command1", "command2", "-e"},
args: []string{"command1", "command2", "-e"},
targetValue: 2,
}, {
name: "test",
args: []string{"test", "-a", "command3"},
args: []string{"-a", "command3"},
targetValue: 1,
},
}
......
......@@ -5,7 +5,7 @@ import (
"strings"
)
func (s *setting[C]) assignValues(c cmd[C]) {
func (s *Settings[C]) assignValues(c cmd[C]) {
flgs := c.flagSet
flgs.Visit(func(f *flag.Flag) {
......
......@@ -5,7 +5,7 @@ import (
"testing"
)
type testInterfaceCallbacks func(s *setting[testIntegrationStruct])
type testInterfaceCallbacks func(s *Settings[testIntegrationStruct])
type testIntegrationStruct struct {
Help bool `short:"h" long:"help" description:"Show this help message"`
......@@ -24,7 +24,7 @@ func TestIntegrationError(t *testing.T) {
tests := []testIntegrationTestCases[testIntegrationStruct]{
{
name: "test",
args: []string{"test2", "-a"},
args: []string{"-a"},
errors: []string{
"flag provided but not defined: -a",
},
......@@ -60,8 +60,8 @@ func TestIntegration(t *testing.T) {
tests := []testIntegrationTestCases[testIntegrationStruct]{
{
name: "test",
args: []string{"test"},
test: func(s *setting[testIntegrationStruct]) {
args: []string{},
test: func(s *Settings[testIntegrationStruct]) {
v := s.GetValues()
assert.NotNil(t, v)
assert.IsType(t, testIntegrationStruct{}, v)
......@@ -71,8 +71,8 @@ func TestIntegration(t *testing.T) {
},
{
name: "test",
args: []string{"test", "-v"},
test: func(s *setting[testIntegrationStruct]) {
args: []string{"-v"},
test: func(s *Settings[testIntegrationStruct]) {
v := s.GetValues()
assert.NotNil(t, v)
assert.IsType(t, testIntegrationStruct{}, v)
......@@ -82,29 +82,29 @@ func TestIntegration(t *testing.T) {
},
{
name: "test",
args: []string{"test", "--verbose"},
test: func(s *setting[testIntegrationStruct]) {
args: []string{"--verbose"},
test: func(s *Settings[testIntegrationStruct]) {
assert.True(t, s.GetValues().Verbose)
},
},
{
name: "test",
args: []string{"test", "-verbose"},
test: func(s *setting[testIntegrationStruct]) {
args: []string{"-verbose"},
test: func(s *Settings[testIntegrationStruct]) {
assert.True(t, s.GetValues().Verbose)
},
},
{
name: "test",
args: []string{"test", "sub1"},
test: func(s *setting[testIntegrationStruct]) {
args: []string{"sub1"},
test: func(s *Settings[testIntegrationStruct]) {
assert.False(t, s.GetValues().Verbose)
},
},
}
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{})
assert.NotNil(t, settings)
......
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.
func (s *setting[C]) Parse(args []string) *setting[C] {
func (s *Settings[C]) Parse(args []string) *Settings[C] {
if len(s.errors) > 0 {
return s
}
......@@ -11,7 +18,7 @@ func (s *setting[C]) Parse(args []string) *setting[C] {
return s
}
err := s.command.flagSet.Parse(args[1:])
err := s.command.flagSet.Parse(args)
if err != nil {
s.errors = append(s.errors, err)
return s
......
......@@ -13,13 +13,13 @@ type Definition struct {
} `command:"serve" description:"Run the HTTP server" call:"DoServe"`
}
func (d *Definition) DoServe(_ *setting[Definition]) {
func (d *Definition) DoServe(_ *Settings[Definition]) {
// do something
}
func TestReadMeInit(t *testing.T) {
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()
assert.True(t, setting.definitions.Verbose)
assert.False(t, setting.HasErrors())
......
{"version":"1.1.1"}
{"version":"1.2.0"}
......@@ -5,7 +5,7 @@ import (
"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.parseStruct(s.definitions)
}
......@@ -14,7 +14,7 @@ type config struct {
errorHandling flag.ErrorHandling
}
type setting[C any] struct {
type Settings[C any] struct {
definitions C
command *cmd[C]
......@@ -29,6 +29,6 @@ type setting[C any] struct {
shadow any
}
func (s *setting[C]) GetValues() C {
func (s *Settings[C]) GetValues() C {
return s.definitions
}
......@@ -6,14 +6,14 @@ import (
)
func TestGetValues(t *testing.T) {
s := &setting[CmdTest1]{
s := &Settings[CmdTest1]{
definitions: CmdTest1{},
}
assert.Equal(t, CmdTest1{}, s.GetValues())
}
func TestInitCommands(t *testing.T) {
s := &setting[CmdTest1]{
s := &Settings[CmdTest1]{
definitions: CmdTest1{},
}
......
......@@ -3,7 +3,7 @@ package xflags
import "reflect"
// 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 {
s.errors = append(s.errors, ShadowMustBePointerError)
......
......@@ -35,7 +35,7 @@ func TestFlagCopyToShadow(t *testing.T) {
settings.SetShadow(&c)
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.ValCommand1Flag2)
......