diff --git a/example_parse-for_test.go b/example_parse-for_test.go index dd2d209..a2be38b 100644 --- a/example_parse-for_test.go +++ b/example_parse-for_test.go @@ -279,7 +279,7 @@ func ExampleMakeOptCfgsFor() { // optCfgs[1].ArgInHelp = // // optCfgs[2].StoreKey = Qux - // optCfgs[2].Names = [Qux] + // optCfgs[2].Names = [] // optCfgs[2].HasArg = true // optCfgs[2].IsArray = false // optCfgs[2].Defaults = [XXX] @@ -295,7 +295,7 @@ func ExampleMakeOptCfgsFor() { // optCfgs[3].ArgInHelp = // // optCfgs[4].StoreKey = Corge - // optCfgs[4].Names = [Corge] + // optCfgs[4].Names = [] // optCfgs[4].HasArg = true // optCfgs[4].IsArray = true // optCfgs[4].Defaults = [] diff --git a/parse-for.go b/parse-for.go index 7dd1684..25e721c 100644 --- a/parse-for.go +++ b/parse-for.go @@ -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 } @@ -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 diff --git a/parse-for_test.go b/parse-for_test.go index aaf0b12..7abd3d0 100644 --- a/parse-for_test.go +++ b/parse-for_test.go @@ -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"}) @@ -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))