From ca13333d25b7bf2761c55a80899308070ff73030 Mon Sep 17 00:00:00 2001
From: Dave Collins <davec@conformal.com>
Date: Tue, 19 Aug 2014 20:06:03 -0500
Subject: [PATCH] Always show help on help flag.

This commit resolves a minor issue where an error in the config file would
prevent the help from being shown until the config file error was
resolved.

This results in the following behavior:

- With a malformed header:
  $ ./btcd
  Error parsing config file: ~/btcd/btcd.conf:14: malformed section header
  Use btcd -h to show usage
- With an invalid option:
  $ ./btcd
  Error parsing config file: unknown option: bogus
  Use btcd -h to show usage
- Invoking help with an error in the config file:
  $ ./btcd -h
  Usage:
  ...
- Invoking with an invalid command line option:
  $ ./btcd --bogus=bogus
  unknown flag `bogus'
  Use btcd -h to show usage

ok @jrick
---
 config.go | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/config.go b/config.go
index 89e0916c..f72d7507 100644
--- a/config.go
+++ b/config.go
@@ -326,11 +326,18 @@ func loadConfig() (*config, []string, error) {
 	}
 
 	// Pre-parse the command line options to see if an alternative config
-	// file or the version flag was specified.  Any errors can be ignored
-	// here since they will be caught be the final parse below.
+	// file or the version flag was specified.  Any errors aside from the
+	// help message error can be ignored here since they will be caught by
+	// the final parse below.
 	preCfg := cfg
-	preParser := newConfigParser(&preCfg, &serviceOpts, flags.None)
-	preParser.Parse()
+	preParser := newConfigParser(&preCfg, &serviceOpts, flags.HelpFlag)
+	_, err = preParser.Parse()
+	if err != nil {
+		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
+			fmt.Fprintln(os.Stderr, err)
+			return nil, nil, err
+		}
+	}
 
 	// Show the version and exit if the version flag was specified.
 	appName := filepath.Base(os.Args[0])