From 51337ded775c91dc26574d7d2dc2e493cf416138 Mon Sep 17 00:00:00 2001 From: "Wlazlo, Matt" Date: Mon, 16 Apr 2018 11:07:48 +1000 Subject: [PATCH] fixed example comment, test coverage issue --- README.md | 2 +- usage_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f3d1bfc..e8b62a4 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,7 @@ func (n *NameDotName) UnmarshalText(b []byte) error { return nil } -// optional, implement in case you want to use defaults +// optional: implement in case you want to display a default value in the usage string func (n *NameDotName) MarshalText() (text []byte, err error) { text = []byte(fmt.Sprintf("%s.%s", n.Head, n.Tail)) return diff --git a/usage_test.go b/usage_test.go index 4aa2f24..4f179f0 100644 --- a/usage_test.go +++ b/usage_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "strings" "fmt" + "errors" ) type NameDotName struct { @@ -85,6 +86,42 @@ Options: assert.Equal(t, expectedHelp, help.String()) } +type MyEnum int + +func (n *MyEnum) UnmarshalText(b []byte) error { + b = []byte("Hello") + return nil +} + +func (n *MyEnum) MarshalText() (text []byte, err error) { + s := "There was a problem" + text = []byte(s) + err = errors.New(s) + return +} + +func TestUsageError(t *testing.T) { + expectedHelp := `Usage: example [--name NAME] + +Options: + --name NAME [default: error: There was a problem] + --help, -h display this help and exit +` + var args struct { + Name *MyEnum + } + v := MyEnum(42) + args.Name = &v + p, err := NewParser(Config{"example"}, &args) + + // NB: some might might expect there to be an error here + require.NoError(t, err) + + var help bytes.Buffer + p.WriteHelp(&help) + assert.Equal(t, expectedHelp, help.String()) +} + func TestUsageLongPositionalWithHelp_legacyForm(t *testing.T) { expectedHelp := `Usage: example VERYLONGPOSITIONALWITHHELP