Skip to content

Commit

Permalink
Merge pull request #231 from yaegashi/fix-default-mask
Browse files Browse the repository at this point in the history
Correctly handle `default-mask:"-"`
  • Loading branch information
jessevdk committed Jul 14, 2017
2 parents e89e5c4 + c453bfc commit 75a96bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions help.go
Expand Up @@ -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
}
Expand Down
64 changes: 64 additions & 0 deletions help_test.go
@@ -1,10 +1,12 @@
package flags

import (
"bufio"
"bytes"
"fmt"
"os"
"runtime"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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())
}
}
}

0 comments on commit 75a96bc

Please sign in to comment.