From f269d593c7459d012da22f9c69cfbcb845d7fe6c Mon Sep 17 00:00:00 2001 From: Chunzhu Li Date: Tue, 10 Nov 2020 01:57:37 -0600 Subject: [PATCH] set --help exit code to zero (#197) * set --help exit code to zero * address comment --- cmd/dumpling/main.go | 10 +++++++--- v4/export/config.go | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/dumpling/main.go b/cmd/dumpling/main.go index 103d72e163bef..b578499535d6e 100644 --- a/cmd/dumpling/main.go +++ b/cmd/dumpling/main.go @@ -15,7 +15,6 @@ package main import ( "context" - "errors" "fmt" _ "net/http/pprof" "os" @@ -33,14 +32,19 @@ func main() { fmt.Fprint(os.Stderr, "Dumpling is a CLI tool that helps you dump MySQL/TiDB data\n\nUsage:\n dumpling [flags]\n\nFlags:\n") pflag.PrintDefaults() } - pflag.ErrHelp = errors.New("") - printVersion := pflag.BoolP("version", "V", false, "Print Dumpling version") conf := export.DefaultConfig() conf.DefineFlags(pflag.CommandLine) pflag.Parse() + if printHelp, err := pflag.CommandLine.GetBool(export.FlagHelp); printHelp || err != nil { + if err != nil { + fmt.Printf("\nGet help flag error: %s\n", err) + } + pflag.Usage() + return + } println(cli.LongVersion()) if *printVersion { return diff --git a/v4/export/config.go b/v4/export/config.go index b6b2b954194e3..12ef01acc54f6 100644 --- a/v4/export/config.go +++ b/v4/export/config.go @@ -62,6 +62,7 @@ const ( flagOutputFilenameTemplate = "output-filename-template" flagCompleteInsert = "complete-insert" flagParams = "params" + FlagHelp = "help" ) type Config struct { @@ -220,6 +221,7 @@ func (conf *Config) DefineFlags(flags *pflag.FlagSet) { flags.String(flagOutputFilenameTemplate, "", "The output filename template (without file extension)") flags.Bool(flagCompleteInsert, false, "Use complete INSERT statements that include column names") flags.StringToString(flagParams, nil, `Extra session variables used while dumping, accepted format: --params "character_set_client=latin1,character_set_connection=latin1"`) + flags.Bool(FlagHelp, false, "Print help message and quit") } // GetDSN generates DSN from Config