diff --git a/api.go b/api.go index 50dbaf00fdc3901f7848569313b4de36bece4d71..ddc7e43a7775146b753ca2baca958966ea39a7b8 100644 --- a/api.go +++ b/api.go @@ -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 -//} diff --git a/command.go b/command.go index 278c038bfe6f28ece3fcce135633fbdd0b916127..cbfcbe7b2a94f5157f163364cb3831adec1a3a40 100644 --- a/command.go +++ b/command.go @@ -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), diff --git a/command_test.go b/command_test.go index 8ecdf9c2333f1824b9e5213c856bf6e0c5b61cfb..e44467c664e9613777f2d6a6acf31ba16158b1ca 100644 --- a/command_test.go +++ b/command_test.go @@ -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]{} diff --git a/error.go b/error.go index a31a283c80f0026be723f1c249c3064d250c25d2..be4a46f0bcd21d6855cacff299185ae084fa307e 100644 --- a/error.go +++ b/error.go @@ -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 } diff --git a/execute.go b/execute.go index 1878761c7fa57104238567c5d061205766f75182..d39e3c766b57d214dce6cfd818414ae53a38dcbd 100644 --- a/execute.go +++ b/execute.go @@ -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 } diff --git a/execute_test.go b/execute_test.go index 2625eb73697b2282eb1a074633d23f20f9423f49..43335b8ff95a3cc010ae91d8f6b2094e214c3909 100644 --- a/execute_test.go +++ b/execute_test.go @@ -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++ } diff --git a/flag.go b/flag.go index eb9f08c699b31bedfaae517a6bc08d9fe2381c11..2f2d8b4186d52ab0474da6ec8dbf872b9e421956 100644 --- a/flag.go +++ b/flag.go @@ -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) { diff --git a/integration_test.go b/integration_test.go index b7ff83ef9634074ae55b16385874641e6c01e2cf..3b076d9c01e237a5f5fff5487a5befec68048fc2 100644 --- a/integration_test.go +++ b/integration_test.go @@ -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"` diff --git a/parse.go b/parse.go index 24c0e26b935144aa3c2e00eb657f1248e3b16383..729f091bd2356f59d0061b8bb73c1649046fbb4b 100644 --- a/parse.go +++ b/parse.go @@ -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 } diff --git a/setting.go b/setting.go index 4a73c4b0f40770ce12988e13324027b877f34780..a87a95466a2fb8653d64735111ae7a9824134104 100644 --- a/setting.go +++ b/setting.go @@ -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 } diff --git a/setting_test.go b/setting_test.go index 23b1265b20a5d6c204fb0e17eec8b03307836410..9e7c7a58be5110a70d292d6ba9f4eea297d070fa 100644 --- a/setting_test.go +++ b/setting_test.go @@ -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{}, } diff --git a/shadow.go b/shadow.go index b41717f1cbcb0dbfc679fa93def87a91be44c469..2aae3a8568703ae56448c4b4a64bbb52247b505b 100644 --- a/shadow.go +++ b/shadow.go @@ -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)