From 20c6561b801e3e929f8cd12745b550bf9daf96c5 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Sun, 18 Dec 2022 18:18:53 +0100
Subject: [PATCH] feat: split help and contextHelp

---
 execute.go      | 32 --------------------------------
 execute_test.go | 31 +++++++++++++++++++++++++++++++
 help.go         | 13 ++++++++++---
 3 files changed, 41 insertions(+), 35 deletions(-)

diff --git a/execute.go b/execute.go
index 994e1eb..06b96d4 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 5275ca7..04d5e25 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 bd8559b..9a7687f 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)
+
+}
-- 
GitLab