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

Adding WroteHelp() helper #263

Merged
merged 1 commit into from
Sep 23, 2018
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
20 changes: 20 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,23 @@ func (p *Parser) WriteHelp(writer io.Writer) {

wr.Flush()
}

// WroteHelp is a helper to test the error from ParseArgs() to
// determine if the help message was written. It is safe to
// call without first checking that error is nil.
func WroteHelp(err error) bool {
if err == nil { // No error
return false
}

flagError, ok := err.(*Error)
if !ok { // Not a go-flag error
return false
}

if flagError.Type != ErrHelp { // Did not print the help message
return false
}

return true
}
23 changes: 23 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flags
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -536,3 +537,25 @@ func TestHelpDefaultMask(t *testing.T) {
}
}
}

func TestWroteHelp(t *testing.T) {
type testInfo struct {
value error
isHelp bool
}
tests := map[string]testInfo{
"No error": testInfo{value: nil, isHelp: false},
"Plain error": testInfo{value: errors.New("an error"), isHelp: false},
"ErrUnknown": testInfo{value: newError(ErrUnknown, "an error"), isHelp: false},
"ErrHelp": testInfo{value: newError(ErrHelp, "an error"), isHelp: true},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := WroteHelp(test.value)
if test.isHelp != res {
t.Errorf("Expected %t, got %t", test.isHelp, res)
}
})
}
}