From 3b94120652d6cb83482d052fdecb1d3e96df6007 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Wed, 5 Oct 2022 21:20:24 +0200 Subject: [PATCH] fix parse only the arguments and not the programm --- api_test.go | 2 +- execute_test.go | 6 +++--- integration_test.go | 24 ++++++++++++------------ parse.go | 2 +- readme_test.go | 4 ++-- shadow_test.go | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/api_test.go b/api_test.go index 2ec6c88..dc7adc9 100644 --- a/api_test.go +++ b/api_test.go @@ -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) diff --git a/execute_test.go b/execute_test.go index a6ecbb4..2625eb7 100644 --- a/execute_test.go +++ b/execute_test.go @@ -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, }, } diff --git a/integration_test.go b/integration_test.go index 3021bb7..b7ff83e 100644 --- a/integration_test.go +++ b/integration_test.go @@ -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) diff --git a/parse.go b/parse.go index bdee7a1..24c0e26 100644 --- a/parse.go +++ b/parse.go @@ -18,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 diff --git a/readme_test.go b/readme_test.go index c1554ba..57e774b 100644 --- a/readme_test.go +++ b/readme_test.go @@ -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()) diff --git a/shadow_test.go b/shadow_test.go index 8418e16..debcc1b 100644 --- a/shadow_test.go +++ b/shadow_test.go @@ -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) -- GitLab