Skip to content

Commit

Permalink
Output --help message to stdout instead of stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
jessevdk committed Nov 4, 2016
1 parent 644fb71 commit e1bd227
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
// -h and --help options. When either -h or --help is specified on the
// command line, the parser will return the special error of type
// ErrHelp. When PrintErrors is also specified, then the help message
// will also be automatically printed to os.Stderr.
// will also be automatically printed to os.Stdout.
HelpFlag = 1 << iota

// PassDoubleDash passes all arguments after a double dash, --, as
Expand All @@ -100,7 +100,8 @@ const (
IgnoreUnknown

// PrintErrors prints any errors which occurred during parsing to
// os.Stderr.
// os.Stderr. In the special case of ErrHelp, the message will be printed
// to os.Stdout.
PrintErrors

// PassAfterNonOption passes all arguments after the first non option
Expand Down Expand Up @@ -676,7 +677,13 @@ func (p *Parser) showBuiltinHelp() error {

func (p *Parser) printError(err error) error {
if err != nil && (p.Options&PrintErrors) != None {
fmt.Fprintln(os.Stderr, err)
flagsErr, ok := err.(*Error)

if ok && flagsErr.Type == ErrHelp {
fmt.Fprintln(os.Stdout, err)
} else {
fmt.Fprintln(os.Stderr, err)
}
}

return err
Expand Down

0 comments on commit e1bd227

Please sign in to comment.