From cf88391d035ae38ef87123c32b6f6d4ce9b1ee56 Mon Sep 17 00:00:00 2001 From: Takbir Newaz Date: Sun, 12 Dec 2021 16:18:20 +0600 Subject: [PATCH] add deprecated flags once again This way, users calling -s or -r get a helpful message. The use of -s can still let the tool work, for now. Fixes #174. --- gofmt.go | 42 ++++++++++++++++++++------- testdata/scripts/deprecated-flags.txt | 26 +++++++++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 testdata/scripts/deprecated-flags.txt diff --git a/gofmt.go b/gofmt.go index 58a63e6..d7391bb 100644 --- a/gofmt.go +++ b/gofmt.go @@ -30,18 +30,22 @@ import ( var ( // main operation modes - list = flag.Bool("l", false, "list files whose formatting differs from gofumpt's") - write = flag.Bool("w", false, "write result to (source) file instead of stdout") - doDiff = flag.Bool("d", false, "display diffs instead of rewriting files") - allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)") + list = flag.Bool("l", false, "") + write = flag.Bool("w", false, "") + doDiff = flag.Bool("d", false, "") + allErrors = flag.Bool("e", false, "") // debugging - cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file") + cpuprofile = flag.String("cpuprofile", "", "") // gofumpt's own flags - langVersion = flag.String("lang", "", "target Go version in the form 1.X (default from go.mod)") - extraRules = flag.Bool("extra", false, "enable extra rules which should be vetted by a human") - showVersion = flag.Bool("version", false, "show version and exit") + langVersion = flag.String("lang", "", "") + extraRules = flag.Bool("extra", false, "") + showVersion = flag.Bool("version", false, "") + + // DEPRECATED + rewriteRule = flag.String("r", "", "") + simplifyAST = flag.Bool("s", false, "") ) // Keep these in sync with go/format/format.go. @@ -71,8 +75,18 @@ func report(err error) { } func usage() { - fmt.Fprintf(os.Stderr, "usage: gofumpt [flags] [path ...]\n") - flag.PrintDefaults() + fmt.Fprintf(os.Stderr, `usage: gofumpt [flags] [path ...] + -version show version and exit + + -d display diffs instead of rewriting files + -e report all errors (not just the first 10 on different lines) + -l list files whose formatting differs from gofumpt's + -w write result to (source) file instead of stdout + -extra enable extra rules which should be vetted by a human + + -cpuprofile str write cpu profile to this file + -lang str target Go version in the form 1.X (default from go.mod) +`) } func initParserMode() { @@ -204,6 +218,14 @@ func gofumptMain() { flag.Usage = usage flag.Parse() + if *simplifyAST { + fmt.Fprintf(os.Stderr, "warning: -s is deprecated as it is always enabled\n") + } + if *rewriteRule != "" { + fmt.Fprintf(os.Stderr, `the rewrite flag is no longer available; use "gofmt -r" instead`+"\n") + os.Exit(2) + } + // Print the gofumpt version if the user asks for it. if *showVersion { version.Print() diff --git a/testdata/scripts/deprecated-flags.txt b/testdata/scripts/deprecated-flags.txt new file mode 100644 index 0000000..0989b40 --- /dev/null +++ b/testdata/scripts/deprecated-flags.txt @@ -0,0 +1,26 @@ +cp foo.orig.go foo.go +! gofumpt -w -r foo foo.go +stderr 'the rewrite flag is no longer available; use "gofmt -r" instead\n' +cmp foo.orig.go foo.go + +gofumpt -w -s foo.go +stderr 'warning: -s is deprecated as it is always enabled\n' +cmp foo.go foo.go.golden + +gofumpt -d foo.go.golden +! stdout . + +-- foo.orig.go -- +package p + +func f() { + + println("foo") + +} +-- foo.go.golden -- +package p + +func f() { + println("foo") +}