@@ -2,6 +2,7 @@ package aperture
22
33import (
44 "crypto/tls"
5+ "errors"
56 "fmt"
67 "io"
78 "io/ioutil"
@@ -11,6 +12,7 @@ import (
1112 "sync"
1213 "time"
1314
15+ flags "github.com/jessevdk/go-flags"
1416 "github.com/lightninglabs/aperture/auth"
1517 "github.com/lightninglabs/aperture/mint"
1618 "github.com/lightninglabs/aperture/proxy"
@@ -71,10 +73,21 @@ var (
7173func Main () {
7274 // TODO: Prevent from running twice.
7375 err := run ()
74- if err != nil {
75- _ , _ = fmt .Fprintln (os .Stderr , err )
76- os .Exit (1 )
76+
77+ // Unwrap our error and check whether help was requested from our flag
78+ // library. If the error is not wrapped, Unwrap returns nil. It is
79+ // still safe to check the type of this nil error.
80+ flagErr , isFlagErr := errors .Unwrap (err ).(* flags.Error )
81+ isHelpErr := isFlagErr && flagErr .Type == flags .ErrHelp
82+
83+ // If we got a nil error, or help was requested, just exit.
84+ if err == nil || isHelpErr {
85+ os .Exit (0 )
7786 }
87+
88+ // Print any other non-help related errors.
89+ _ , _ = fmt .Fprintln (os .Stderr , err )
90+ os .Exit (1 )
7891}
7992
8093// run sets up the proxy server and runs it. This function blocks until a
@@ -91,7 +104,7 @@ func run() error {
91104 configFile := filepath .Join (apertureDataDir , defaultConfigFilename )
92105 cfg , err := getConfig (configFile )
93106 if err != nil {
94- return fmt .Errorf ("unable to parse config file: %v " , err )
107+ return fmt .Errorf ("unable to parse config file: %w " , err )
95108 }
96109 err = setupLogging (cfg , interceptor )
97110 if err != nil {
@@ -316,6 +329,12 @@ func getConfig(configFile string) (*Config, error) {
316329 return nil , err
317330 }
318331
332+ // Finally, parse the remaining command line options again to ensure
333+ // they take precedence.
334+ if _ , err := flags .Parse (cfg ); err != nil {
335+ return nil , err
336+ }
337+
319338 // Then check the configuration that we got from the config file, all
320339 // required values need to be set at this point.
321340 if cfg .ListenAddr == "" {
0 commit comments