diff --git a/execute.go b/execute.go index 994e1eb19de126b784bd12de9dd2b7440df2846f..06b96d42e891fc88f7b0ebf290a4b30447ba0fb3 100644 --- a/execute.go +++ b/execute.go @@ -6,10 +6,8 @@ package xflags import ( "flag" "fmt" - "github.com/stretchr/testify/assert" "reflect" "strings" - "testing" ) func (s *Settings[C]) Execute() *Settings[C] { @@ -118,33 +116,3 @@ func (s *Settings[C]) MissingCommand() bool { func (s *Settings[C]) WasExecuted() bool { return s.wasExecuted } - -func TestWrongDefinitionType(t *testing.T) { - c := New("root", 2) - c.Parse([]string{"test"}) - c.Execute() - assert.True(t, c.HasErrors()) -} - -type testExecuteCommandStruct struct { - Command1 struct { - } `command:"command1" description:"Command 1" callback:"command1Callback" ` - Command2 struct { - Command3 struct { - } `command:"command3" description:"Command 3" callback:"command3Callback" call:"DoCmd3"` - } `command:"command2" description:"Command 2" callback:"command2Callback" ` -} - -func (c *testExecuteCommandStruct) DoCmd3(s *Settings[testExecuteCommandStruct]) { -} - -func (c *testExecuteCommandStruct) command1Callback(args []string) { - fmt.Println("command1Callback", args) -} - -func TestExecute1(t *testing.T) { - c := New("root", testExecuteCommandStruct{}) - c.Parse([]string{"command2", "command3", "commandX"}) - c.Execute() - assert.False(t, c.HasErrors()) -} diff --git a/execute_test.go b/execute_test.go index 5275ca7051edf0128dc22446b6acfe62a5e80b0c..04d5e257278db2df5dd03ab3462b8fd27b8c2860 100644 --- a/execute_test.go +++ b/execute_test.go @@ -4,11 +4,42 @@ package xflags import ( + "fmt" "github.com/stretchr/testify/assert" "strconv" "testing" ) +func TestWrongDefinitionType(t *testing.T) { + c := New("root", 2) + c.Parse([]string{"test"}) + c.Execute() + assert.True(t, c.HasErrors()) +} + +type testExecuteCommandStruct struct { + Command1 struct { + } `command:"command1" description:"Command 1" callback:"command1Callback" ` + Command2 struct { + Command3 struct { + } `command:"command3" description:"Command 3" callback:"command3Callback" call:"DoCmd3"` + } `command:"command2" description:"Command 2" callback:"command2Callback" ` +} + +func (c *testExecuteCommandStruct) DoCmd3(s *Settings[testExecuteCommandStruct]) { +} + +func (c *testExecuteCommandStruct) command1Callback(args []string) { + fmt.Println("command1Callback", args) +} + +func TestExecute1(t *testing.T) { + c := New("root", testExecuteCommandStruct{}) + c.Parse([]string{"command2", "command3", "commandX"}) + c.Execute() + assert.False(t, c.HasErrors()) +} + type testExecutionStruct struct { callbackCounter int `ignore:"true"` diff --git a/help.go b/help.go index bd8559b3f6724ccfc988f2c3ebcf1b80af57fa2e..9a7687f947300fe9f9455477c8dab896324e2493 100644 --- a/help.go +++ b/help.go @@ -27,10 +27,11 @@ func (c *cmd[C]) getCommandLevel() (*cmd[C], []string) { return result, path } -// Help returns the help text for the command func (s *Settings[C]) Help() string { + return s.createHelp(s.command, []string{}) +} - cmd, path := s.command.getCommandLevel() +func (s *Settings[C]) createHelp(cmd *cmd[C], path []string) string { h := strings.Join(path, " ") var help string @@ -67,6 +68,12 @@ func (s *Settings[C]) Help() string { } } - return help } + +// Help returns the help text for the command +func (s *Settings[C]) ContextHelp() string { + cmd, path := s.command.getCommandLevel() + return s.createHelp(cmd, path) + +}