Skip to content

Commit

Permalink
[option] fix print helper msg twice bug
Browse files Browse the repository at this point in the history
  • Loading branch information
benja-wu authored and xxx7xxxx committed Jun 18, 2021
1 parent 7bae03f commit 0c189ac
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Options struct {

// Flags from command line only.
ShowVersion bool `yaml:"-"`
ShowHelp bool `yaml:"-"`
ShowConfig bool `yaml:"-"`
ConfigFile string `yaml:"-"`
ForceNewCluster bool `yaml:"-"`
Expand Down Expand Up @@ -90,6 +91,7 @@ func New() *Options {
}

opt.flags.BoolVarP(&opt.ShowVersion, "version", "v", false, "Print the version and exit.")
opt.flags.BoolVarP(&opt.ShowHelp, "help", "h", false, "Print the helper message and exit.")
opt.flags.BoolVarP(&opt.ShowConfig, "print-config", "c", false, "Print the configuration.")
opt.flags.StringVarP(&opt.ConfigFile, "config-file", "f", "", "Load server configuration from a file(yaml format), other command line flags will be ignored if specified.")
opt.flags.BoolVar(&opt.ForceNewCluster, "force-new-cluster", false, "Force to create a new one-member cluster.")
Expand Down Expand Up @@ -129,17 +131,17 @@ func (opt *Options) YAML() string {
func (opt *Options) Parse() (string, error) {
err := opt.flags.Parse(os.Args[1:])
if err != nil {
if err == pflag.ErrHelp {
return opt.flags.FlagUsages(), nil
} else {
return "", nil
}
return "", err
}

if opt.ShowVersion {
return version.Short, nil
}

if opt.ShowHelp {
return opt.flags.FlagUsages(), nil
}

opt.viper.AutomaticEnv()
opt.viper.SetEnvPrefix("EG")
opt.viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down

0 comments on commit 0c189ac

Please sign in to comment.