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

Custom flag's usage string shouldn't change its "defaults" section after parsing #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (f *Flag) defaultIsZeroValue() bool {
case *intSliceValue, *stringSliceValue, *stringArrayValue:
return f.DefValue == "[]"
default:
switch f.Value.String() {
switch f.DefValue {
case "false":
return true
case "<nil>":
Expand Down
36 changes: 36 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,3 +1262,39 @@ func TestVisitFlagOrder(t *testing.T) {
i++
})
}

// TestCustomFlagValue verifies that custom flag usage string doesn't change its "default" section after parsing
func TestCustomFlagDefValue(t *testing.T) {
fs := NewFlagSet("TestCustomFlagDefValue", ContinueOnError)
var buf bytes.Buffer
fs.SetOutput(&buf)

var cv customValue
fs.VarP(&cv, "customP", "", "a VarP with no default")

fs.PrintDefaults()
beforeParse := buf.String()
buf.Reset()

args := []string{
"--customP=10",
}

if err := fs.Parse(args); err != nil {
t.Error("expected no error, got ", err)
}

val := fs.Lookup("customP").Value.String()
if val != "10" {
t.Errorf("expected customP to be set to the new value, got %s\n", val)
}

fs.PrintDefaults()
afterParse := buf.String()

if beforeParse != afterParse {
fmt.Println("\n" + beforeParse)
fmt.Println("\n" + afterParse)
t.Errorf("got %q want %q\n", afterParse, beforeParse)
}
}