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

feat: split help and contextHelp

parent 6a34b021
No related branches found
No related tags found
No related merge requests found
......@@ -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())
}
......@@ -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"`
......
......@@ -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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment