Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modified MakeOptCfgsFor to set Names empty if optcfg has no opt name #62

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example_parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func ExampleMakeOptCfgsFor() {
// optCfgs[1].ArgInHelp = <number>
//
// optCfgs[2].StoreKey = Qux
// optCfgs[2].Names = [Qux]
// optCfgs[2].Names = []
// optCfgs[2].HasArg = true
// optCfgs[2].IsArray = false
// optCfgs[2].Defaults = [XXX]
Expand All @@ -295,7 +295,7 @@ func ExampleMakeOptCfgsFor() {
// optCfgs[3].ArgInHelp = <array elem>
//
// optCfgs[4].StoreKey = Corge
// optCfgs[4].Names = [Corge]
// optCfgs[4].Names = []
// optCfgs[4].HasArg = true
// optCfgs[4].IsArray = true
// optCfgs[4].Defaults = []
Expand Down
11 changes: 9 additions & 2 deletions parse-for.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ func MakeOptCfgsFor(options any) ([]OptCfg, error) {
for i := 0; i < n; i++ {
optCfgs[i] = newOptCfg(t.Field(i))

setter, err := newValueSetter(optCfgs[i].Names[0], t.Field(i).Name, v.Field(i))
var optName string
if len(optCfgs[i].Names) > 0 {
optName = optCfgs[i].Names[0]
} else {
optName = optCfgs[i].StoreKey
}

setter, err := newValueSetter(optName, t.Field(i).Name, v.Field(i))
if err != nil {
return nil, err
}
Expand All @@ -118,7 +125,7 @@ func newOptCfg(fld reflect.StructField) OptCfg {

names := strings.Split(arr[0], ",")
if len(names) == 0 || len(names[0]) == 0 {
names = []string{storeKey}
names = []string{}
}

isArray := false
Expand Down
4 changes: 2 additions & 2 deletions parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ func TestMakeOptCfgsFor_multipleOptsAndMultipleArgs(t *testing.T) {
assert.False(t, optCfgs[1].IsArray)
assert.Equal(t, optCfgs[1].Defaults, []string{"99"})
assert.Equal(t, optCfgs[2].StoreKey, "Qux")
assert.Equal(t, optCfgs[2].Names, []string{"Qux"})
assert.Equal(t, optCfgs[2].Names, []string{})
assert.True(t, optCfgs[2].HasArg)
assert.False(t, optCfgs[2].IsArray)
assert.Equal(t, optCfgs[2].Defaults, []string{"XXX"})
Expand All @@ -1783,7 +1783,7 @@ func TestMakeOptCfgsFor_multipleOptsAndMultipleArgs(t *testing.T) {
assert.True(t, optCfgs[3].IsArray)
assert.Equal(t, optCfgs[3].Defaults, []string{"A", "B", "C"})
assert.Equal(t, optCfgs[4].StoreKey, "Corge")
assert.Equal(t, optCfgs[4].Names, []string{"Corge"})
assert.Equal(t, optCfgs[4].Names, []string{})
assert.True(t, optCfgs[4].HasArg)
assert.True(t, optCfgs[4].IsArray)
assert.Equal(t, optCfgs[4].Defaults, []string(nil))
Expand Down
Loading