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 EnumVarP func to show default value associated with EnumVariable … #106

Merged
merged 3 commits into from
Feb 16, 2023
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
2 changes: 1 addition & 1 deletion enum_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

func TestEnumVarPositive(t *testing.T) {
flagSet := NewFlagSet()
flagSet.EnumVar(&enumString, "enum", Nil, "enum", AllowdTypes{"type1": Type1, "type2": Type2})
flagSet.EnumVar(&enumString, "enum", Type1, "enum", AllowdTypes{"type1": Type1, "type2": Type2})
os.Args = []string{
os.Args[0],
"--enum", "type1",
Expand Down
7 changes: 6 additions & 1 deletion goflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,20 @@ func (flagSet *FlagSet) EnumVar(field *string, long string, defaultValue EnumVar

// EnumVarP adds a enum flag with a shortname and longname
func (flagSet *FlagSet) EnumVarP(field *string, long, short string, defaultValue EnumVariable, usage string, allowedTypes AllowdTypes) *FlagData {
var hasDefaultValue bool
for k, v := range allowedTypes {
if v == defaultValue {
hasDefaultValue = true
*field = k
}
}
if !hasDefaultValue {
panic("undefined default value")
}
flagData := &FlagData{
usage: usage,
long: long,
defaultValue: defaultValue,
defaultValue: *field,
Mzack9999 marked this conversation as resolved.
Show resolved Hide resolved
}
if short != "" {
flagData.short = short
Expand Down
10 changes: 10 additions & 0 deletions goflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestUsageOrder(t *testing.T) {
var stringSliceData2 StringSlice
var intData int
var boolData bool
var enumData string

flagSet.SetGroup("String", "String")
flagSet.StringVar(&stringData, "string-value", "", "String example value example").Group("String")
Expand All @@ -111,6 +112,13 @@ func TestUsageOrder(t *testing.T) {
flagSet.BoolVar(&boolData, "bool-with-default-value", true, "Bool with default value example").Group("Bool")
flagSet.BoolVarP(&boolData, "bool-with-default-value2", "bwdv", true, "Bool with default value example #2").Group("Bool")

flagSet.SetGroup("Enum", "Enum")
flagSet.EnumVarP(&enumData, "enum-with-default-value", "en", EnumVariable(0), "Enum with default value(zero/one/two)", AllowdTypes{
"zero": EnumVariable(0),
"one": EnumVariable(1),
"two": EnumVariable(2),
}).Group("Enum")

output := &bytes.Buffer{}
flagSet.CommandLine.SetOutput(output)

Expand Down Expand Up @@ -145,6 +153,8 @@ BOOLEAN:
-bv, -bool-value2 Bool value example #2
-bool-with-default-value Bool with default value example (default true)
-bwdv, -bool-with-default-value2 Bool with default value example #2 (default true)
ENUM:
-en, -enum-with-default-value value Enum with default value(zero/one/two) (default zero)
`
assert.Equal(t, expected, actual)

Expand Down