diff --git a/help.go b/help.go index 9167847..d380305 100644 --- a/help.go +++ b/help.go @@ -216,8 +216,10 @@ func (p *Parser) writeHelpOption(writer *bufio.Writer, option *Option, info alig var def string - if len(option.DefaultMask) != 0 && option.DefaultMask != "-" { - def = option.DefaultMask + if len(option.DefaultMask) != 0 { + if option.DefaultMask != "-" { + def = option.DefaultMask + } } else { def = option.defaultLiteral } diff --git a/help_test.go b/help_test.go index 534a51c..bb76640 100644 --- a/help_test.go +++ b/help_test.go @@ -1,10 +1,12 @@ package flags import ( + "bufio" "bytes" "fmt" "os" "runtime" + "strings" "testing" "time" ) @@ -472,3 +474,65 @@ func TestWrapParagraph(t *testing.T) { assertDiff(t, got, expected, "wrapped paragraph") } + +func TestHelpDefaultMask(t *testing.T) { + var tests = []struct { + opts interface{} + present string + }{ + { + opts: &struct { + Value string `short:"v" default:"123" description:"V"` + }{}, + present: "V (default: 123)\n", + }, + { + opts: &struct { + Value string `short:"v" default:"123" default-mask:"abc" description:"V"` + }{}, + present: "V (default: abc)\n", + }, + { + opts: &struct { + Value string `short:"v" default:"123" default-mask:"-" description:"V"` + }{}, + present: "V\n", + }, + { + opts: &struct { + Value string `short:"v" description:"V"` + }{Value: "123"}, + present: "V (default: 123)\n", + }, + { + opts: &struct { + Value string `short:"v" default-mask:"abc" description:"V"` + }{Value: "123"}, + present: "V (default: abc)\n", + }, + { + opts: &struct { + Value string `short:"v" default-mask:"-" description:"V"` + }{Value: "123"}, + present: "V\n", + }, + } + + for _, test := range tests { + p := NewParser(test.opts, HelpFlag) + _, err := p.ParseArgs([]string{"-h"}) + if flagsErr, ok := err.(*Error); ok && flagsErr.Type == ErrHelp { + err = nil + } + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + h := &bytes.Buffer{} + w := bufio.NewWriter(h) + p.writeHelpOption(w, p.FindOptionByShortName('v'), p.getAlignmentInfo()) + w.Flush() + if strings.Index(h.String(), test.present) < 0 { + t.Errorf("Not present %q\n%s", test.present, h.String()) + } + } +}