Only show parse errors and command to invoke usage.

Rather than showing the usage when an error is encounted during options
parsing, show a message that describes how to invoke help instead.  This
is useful because the help is long enough now that the error is often
overlooked since it scrolls out of view.

ok @jrick
This commit is contained in:
Dave Collins 2014-07-14 20:27:22 -05:00
parent 000691dc9e
commit 08c01f08b1

View file

@ -333,9 +333,10 @@ func loadConfig() (*config, []string, error) {
preParser.Parse() preParser.Parse()
// Show the version and exit if the version flag was specified. // Show the version and exit if the version flag was specified.
if preCfg.ShowVersion {
appName := filepath.Base(os.Args[0]) appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName)) appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show usage", appName)
if preCfg.ShowVersion {
fmt.Println(appName, "version", version()) fmt.Println(appName, "version", version())
os.Exit(0) os.Exit(0)
} }
@ -360,8 +361,9 @@ func loadConfig() (*config, []string, error) {
err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile) err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil { if err != nil {
if _, ok := err.(*os.PathError); !ok { if _, ok := err.(*os.PathError); !ok {
fmt.Fprintln(os.Stderr, err) fmt.Fprintf(os.Stderr, "Error parsing config "+
parser.WriteHelp(os.Stderr) "file: %v\n", err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
configFileError = err configFileError = err
@ -377,7 +379,7 @@ func loadConfig() (*config, []string, error) {
remainingArgs, err := parser.Parse() remainingArgs, err := parser.Parse()
if err != nil { if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp { if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
} }
return nil, nil, err return nil, nil, err
} }
@ -399,7 +401,7 @@ func loadConfig() (*config, []string, error) {
"used together -- choose one of the three" "used together -- choose one of the three"
err := fmt.Errorf(str, funcName) err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -443,7 +445,7 @@ func loadConfig() (*config, []string, error) {
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil { if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", funcName, err.Error()) err := fmt.Errorf("%s: %v", funcName, err.Error())
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -453,7 +455,7 @@ func loadConfig() (*config, []string, error) {
"supported types %v" "supported types %v"
err := fmt.Errorf(str, funcName, cfg.DbType, knownDbTypes) err := fmt.Errorf(str, funcName, cfg.DbType, knownDbTypes)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -464,7 +466,7 @@ func loadConfig() (*config, []string, error) {
str := "%s: The profile port must be between 1024 and 65535" str := "%s: The profile port must be between 1024 and 65535"
err := fmt.Errorf(str, funcName) err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
} }
@ -474,7 +476,7 @@ func loadConfig() (*config, []string, error) {
str := "%s: The banduration option may not be less than 1s -- parsed [%v]" str := "%s: The banduration option may not be less than 1s -- parsed [%v]"
err := fmt.Errorf(str, funcName, cfg.BanDuration) err := fmt.Errorf(str, funcName, cfg.BanDuration)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -484,7 +486,7 @@ func loadConfig() (*config, []string, error) {
"mixed" "mixed"
err := fmt.Errorf(str, funcName) err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -536,7 +538,7 @@ func loadConfig() (*config, []string, error) {
err := fmt.Errorf(str, funcName, blockMaxSizeMin, err := fmt.Errorf(str, funcName, blockMaxSizeMin,
blockMaxSizeMax, cfg.BlockMaxSize) blockMaxSizeMax, cfg.BlockMaxSize)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
@ -554,14 +556,14 @@ func loadConfig() (*config, []string, error) {
str := "%s: getworkkey '%s' failed to decode: %v" str := "%s: getworkkey '%s' failed to decode: %v"
err := fmt.Errorf(str, funcName, strAddr, err) err := fmt.Errorf(str, funcName, strAddr, err)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
if !addr.IsForNet(activeNetParams.Params) { if !addr.IsForNet(activeNetParams.Params) {
str := "%s: getworkkey '%s' is on the wrong network" str := "%s: getworkkey '%s' is on the wrong network"
err := fmt.Errorf(str, funcName, strAddr) err := fmt.Errorf(str, funcName, strAddr)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
cfg.miningAddrs = append(cfg.miningAddrs, addr) cfg.miningAddrs = append(cfg.miningAddrs, addr)
@ -574,14 +576,14 @@ func loadConfig() (*config, []string, error) {
str := "%s: mining address '%s' failed to decode: %v" str := "%s: mining address '%s' failed to decode: %v"
err := fmt.Errorf(str, funcName, strAddr, err) err := fmt.Errorf(str, funcName, strAddr, err)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
if !addr.IsForNet(activeNetParams.Params) { if !addr.IsForNet(activeNetParams.Params) {
str := "%s: mining address '%s' is on the wrong network" str := "%s: mining address '%s' is on the wrong network"
err := fmt.Errorf(str, funcName, strAddr) err := fmt.Errorf(str, funcName, strAddr)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }
cfg.miningAddrs = append(cfg.miningAddrs, addr) cfg.miningAddrs = append(cfg.miningAddrs, addr)
@ -594,7 +596,7 @@ func loadConfig() (*config, []string, error) {
"addresses specified " "addresses specified "
err := fmt.Errorf(str, funcName) err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err return nil, nil, err
} }