Skip to content

Commit a645aca

Browse files
jholdstockdavecgh
authored andcommitted
config: Explicitly handle help requests.
Checking for --help as an explicit step before parsing any other configs makes the code more intuitive by removing a convoluted bit of error handling. It also enables the IgnoreUnknown option to be used whilst parsing for help, which ensures the presence of --help will always result in the help message being printed. This fixes a minor inconsistency where the help message would be printed if the flag was placed before an invalid config, but placing it after would cause an invalid config error to be written instead. For example, `dcrd --help --fakeflag` vs `dcrd --fakeflag --help`.
1 parent 1897e8c commit a645aca

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

config.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,10 @@ func (e errSuppressUsage) Error() string {
585585
//
586586
// The configuration proceeds as follows:
587587
// 1. Start with a default config with sane settings
588-
// 2. Pre-parse the command line to check for an alternative config file
589-
// 3. Load configuration file overwriting defaults with any specified options
590-
// 4. Parse CLI options and overwrite/add any specified options
588+
// 2. Check if help has been requested, print it and exit if so
589+
// 3. Pre-parse the command line to check for an alternative config file
590+
// 4. Load configuration file overwriting defaults with any specified options
591+
// 5. Parse CLI options and overwrite/add any specified options
591592
//
592593
// The above results in dcrd functioning properly without any config settings
593594
// while still allowing the user to override settings with config files and
@@ -648,23 +649,23 @@ func loadConfig(appName string) (*config, []string, error) {
648649
params: &mainNetParams,
649650
}
650651

652+
// Pre-parse the command line options looking only for the help option. If
653+
// found, print the help message to stdout and exit.
654+
helpOpts := flags.Options(flags.HelpFlag | flags.PrintErrors | flags.IgnoreUnknown)
655+
_, err := flags.NewParser(&cfg, helpOpts).Parse()
656+
if flags.WroteHelp(err) {
657+
os.Exit(0)
658+
}
659+
651660
// Service options which are only added on Windows.
652661
serviceOpts := serviceOptions{}
653662

654-
// Pre-parse the command line options to see if an alternative config
655-
// file or the version flag was specified. Any errors aside from the
656-
// help message error can be ignored here since they will be caught by
657-
// the final parse below.
663+
// Pre-parse the command line options to see if an alternative config file
664+
// or the version flag was specified. Any errors can be ignored here since
665+
// they will be caught by the final parse below.
658666
preCfg := cfg
659-
preParser := newConfigParser(&preCfg, &serviceOpts, flags.HelpFlag)
660-
_, err := preParser.Parse()
661-
if err != nil {
662-
var e *flags.Error
663-
if errors.As(err, &e) && e.Type == flags.ErrHelp {
664-
fmt.Fprintln(os.Stdout, err)
665-
os.Exit(0)
666-
}
667-
}
667+
preParser := newConfigParser(&preCfg, &serviceOpts, flags.None)
668+
_, _ = preParser.Parse()
668669

669670
// Show the version and exit if the version flag was specified.
670671
if preCfg.ShowVersion {
@@ -739,7 +740,7 @@ func loadConfig(appName string) (*config, []string, error) {
739740

740741
// Load additional config from file.
741742
var configFileError error
742-
parser := newConfigParser(&cfg, &serviceOpts, flags.HelpFlag|flags.PassDoubleDash)
743+
parser := newConfigParser(&cfg, &serviceOpts, flags.PassDoubleDash)
743744
if !(cfg.SimNet || cfg.RegNet) || preCfg.ConfigFile != defaultConfigFile {
744745
err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
745746
if err != nil {

0 commit comments

Comments
 (0)