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

fix Settings should be exported

parent 3b941206
No related branches found
No related tags found
No related merge requests found
......@@ -10,9 +10,9 @@ import (
// New creates a new instance of the settings.
// name should be the name of the command and comes from the first argument of the command line.
// os.Args[0] is a good choice.
func New[C any](name string, definitions C) *setting[C] {
func New[C any](name string, definitions C) *Settings[C] {
s := &setting[C]{
s := &Settings[C]{
config: config{
errorHandling: flag.ContinueOnError,
},
......@@ -32,32 +32,11 @@ func New[C any](name string, definitions C) *setting[C] {
}
// FlagOutput returns the writer where the flag package writes its output.
func (s *setting[C]) FlagOutput() string {
func (s *Settings[C]) FlagOutput() string {
return s.flagOutput.(*bytes.Buffer).String()
}
// Args Returns not parsed arguments.
func (s *setting[C]) Args() []string {
func (s *Settings[C]) Args() []string {
return s.args
}
//type ConfigAny[C any] struct {
// path string
// ConfigurationAdapter[C]
//}
//
//type ConfigurationAdapter[RT any] interface {
// InitFromFlagSet(flagSet *flag.FlagSet) RT
//}
//func (s *setting[C]) CopyValuesToStruct(path string, adapter any) *setting[C] {
//
// if s.command == nil {
// s.errors = append(s.errors, MissingCommandError)
// return s
// }
//
// s.command.copyValuesToStruct(path, adapter)
//
// return s
//}
......@@ -11,7 +11,7 @@ type cmd[C any] struct {
tagMapping map[string]string
shadowMapping map[string]string
commands []*cmd[C]
settings *setting[C]
settings *Settings[C]
valuePath []string
functionName string
}
......@@ -51,7 +51,7 @@ func (c *cmd[C]) parse(args []string) {
}
func buildCommandStruct[C any](s *setting[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] {
func buildCommandStruct[C any](s *Settings[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] {
cc := &cmd[C]{
name: name,
flagSet: flag.NewFlagSet(name, errorHandling),
......
......@@ -30,7 +30,7 @@ func TestParse(t *testing.T) {
t.Run(test.commandline[0], func(t *testing.T) {
c := &cmd[testStructParseCommand1]{}
c.settings = &setting[testStructParseCommand1]{}
c.settings = &Settings[testStructParseCommand1]{}
c.commands = []*cmd[testStructParseCommand1]{}
......
......@@ -7,18 +7,18 @@ import (
// ResetError is used to reset the error to nil
// After calling this function, the call HasErrors() will return false
func (s *setting[C]) ResetErrors() *setting[C] {
func (s *Settings[C]) ResetErrors() *Settings[C] {
s.errors = []error{}
return s
}
// Check if the setting contains errors
func (s *setting[C]) HasErrors() bool {
func (s *Settings[C]) HasErrors() bool {
return len(s.errors) > 0
}
// Get all errors
func (s *setting[C]) Errors() []error {
func (s *Settings[C]) Errors() []error {
return s.errors
}
......
......@@ -4,7 +4,7 @@ import (
"reflect"
)
func (s *setting[C]) Execute() *setting[C] {
func (s *Settings[C]) Execute() *Settings[C] {
if len(s.errors) > 0 {
return s
}
......
......@@ -25,15 +25,15 @@ type testExecutionStruct struct {
} `command:"command3" description:"Command 3" call:"Command3Callback" `
}
func (c *testExecutionStruct) Command1Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command1Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
func (c *testExecutionStruct) Command2Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command2Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
func (c *testExecutionStruct) Command3Callback(s *setting[testExecutionStruct]) {
func (c *testExecutionStruct) Command3Callback(s *Settings[testExecutionStruct]) {
s.definitions.callbackCounter++
}
......
......@@ -5,7 +5,7 @@ import (
"strings"
)
func (s *setting[C]) assignValues(c cmd[C]) {
func (s *Settings[C]) assignValues(c cmd[C]) {
flgs := c.flagSet
flgs.Visit(func(f *flag.Flag) {
......
......@@ -5,7 +5,7 @@ import (
"testing"
)
type testInterfaceCallbacks func(s *setting[testIntegrationStruct])
type testInterfaceCallbacks func(s *Settings[testIntegrationStruct])
type testIntegrationStruct struct {
Help bool `short:"h" long:"help" description:"Show this help message"`
......
......@@ -8,7 +8,7 @@ func (s *Settings[C]) ParseOsArgs() *Settings[C] {
}
// Parse parses the command line arguments and assigns the values to the settings.
func (s *setting[C]) Parse(args []string) *setting[C] {
func (s *Settings[C]) Parse(args []string) *Settings[C] {
if len(s.errors) > 0 {
return s
}
......
......@@ -5,7 +5,7 @@ import (
"io"
)
func (s *setting[C]) initCommands(name string) {
func (s *Settings[C]) initCommands(name string) {
s.command = buildCommandStruct[C](s, name, "", s.config.errorHandling, []string{})
s.command.parseStruct(s.definitions)
}
......@@ -14,7 +14,7 @@ type config struct {
errorHandling flag.ErrorHandling
}
type setting[C any] struct {
type Settings[C any] struct {
definitions C
command *cmd[C]
......@@ -29,6 +29,6 @@ type setting[C any] struct {
shadow any
}
func (s *setting[C]) GetValues() C {
func (s *Settings[C]) GetValues() C {
return s.definitions
}
......@@ -6,14 +6,14 @@ import (
)
func TestGetValues(t *testing.T) {
s := &setting[CmdTest1]{
s := &Settings[CmdTest1]{
definitions: CmdTest1{},
}
assert.Equal(t, CmdTest1{}, s.GetValues())
}
func TestInitCommands(t *testing.T) {
s := &setting[CmdTest1]{
s := &Settings[CmdTest1]{
definitions: CmdTest1{},
}
......
......@@ -3,7 +3,7 @@ package xflags
import "reflect"
// SetShadow sets the shadow struct for the flag configuration.
func (s *setting[C]) SetShadow(shadow any) *setting[C] {
func (s *Settings[C]) SetShadow(shadow any) *Settings[C] {
if reflect.TypeOf(shadow).Kind() != reflect.Ptr {
s.errors = append(s.errors, ShadowMustBePointerError)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment