Output --help message to stdout instead of stderr
diff --git a/parser.go b/parser.go
index 3a55626..fd2fd5f 100644
--- a/parser.go
+++ b/parser.go
@@ -87,7 +87,7 @@
// -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
@@ -100,7 +100,8 @@
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
@@ -676,7 +677,13 @@
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