Skip to content

Commit

Permalink
Always show help on help flag.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davecgh committed Aug 20, 2014
1 parent 3dc3fef commit ca13333
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions config.go
Expand Up @@ -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])
Expand Down

0 comments on commit ca13333

Please sign in to comment.