Skip to content

Commit

Permalink
config: Show usage message on invalid cli flags.
Browse files Browse the repository at this point in the history
This fixes a bug where the error handling code was not matching the
intended behaviour described in its comment, and as a result the usage
message was not being displayed when an invalid config flag was passed.
dcrd was also exiting with code 0 instead of 1.

After fixing this bug the usage message was shown as expected and the
exit code was correct, but the config error was being printed twice -
once by dcrd itself, and once by the go-flags lib. This was
fixed by changing the options passed into go-flags.
  • Loading branch information
jholdstock authored and davecgh committed May 16, 2024
1 parent e075564 commit 318efaa
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2022 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -660,11 +660,7 @@ func loadConfig(appName string) (*config, []string, error) {
_, err := preParser.Parse()
if err != nil {
var e *flags.Error
if errors.As(err, &e) {
if e.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if errors.As(err, &e) && e.Type == flags.ErrHelp {
fmt.Fprintln(os.Stdout, err)
os.Exit(0)
}
Expand Down Expand Up @@ -743,7 +739,7 @@ func loadConfig(appName string) (*config, []string, error) {

// Load additional config from file.
var configFileError error
parser := newConfigParser(&cfg, &serviceOpts, flags.Default)
parser := newConfigParser(&cfg, &serviceOpts, flags.HelpFlag|flags.PassDoubleDash)
if !(cfg.SimNet || cfg.RegNet) || preCfg.ConfigFile != defaultConfigFile {
err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
Expand Down

0 comments on commit 318efaa

Please sign in to comment.