Skip to content
Snippets Groups Projects
Verified Commit 3b941206 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix parse only the arguments and not the programm

parent 549bb819
No related branches found
No related tags found
No related merge requests found
...@@ -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)
......
...@@ -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,
}, },
} }
......
...@@ -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)
......
...@@ -18,7 +18,7 @@ func (s *setting[C]) Parse(args []string) *setting[C] { ...@@ -18,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())
......
...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment