Skip to content

Commit 3496c6f

Browse files
authored
Make flags local to the commands that actually support them. (#607)
Previously, many of the flags were global, instead of local to each command. As a result, it was possible to set flags that are unsupported for a given command. This change makes all flags local, and only adds flags that are actually supported by each command. This improves the output of gopherjs --help and gopherjs <command> --help, because it more accurately maps to the reality of flags and how they're used. This way, flags that are unused by a certain command do not show up in that command's --help output. It's no longer possible to set -w flag with gopherjs serve, which was meaningless (it had no effect, and was confusing). The -w flag is also removed from gopherjs get command. It's unlikely anyone is using -w flag together with gopherjs get command on purpose, since it's pretty loaded and confusing. Instead, people can use gopherjs get, followed by gopherjs install -w to achieve same effect. Fixes #606.
1 parent 0cc212b commit 3496c6f

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

tool.go

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,37 @@ func init() {
5959
}
6060

6161
func main() {
62-
options := &gbuild.Options{CreateMapFile: true}
63-
var pkgObj string
64-
65-
pflag.BoolVarP(&options.Verbose, "verbose", "v", false, "print the names of packages as they are compiled")
66-
flagVerbose := pflag.Lookup("verbose")
67-
pflag.BoolVarP(&options.Quiet, "quiet", "q", false, "suppress non-fatal warnings")
68-
flagQuiet := pflag.Lookup("quiet")
69-
pflag.BoolVarP(&options.Watch, "watch", "w", false, "watch for changes to the source files")
70-
flagWatch := pflag.Lookup("watch")
71-
pflag.BoolVarP(&options.Minify, "minify", "m", false, "minify generated code")
72-
flagMinify := pflag.Lookup("minify")
73-
pflag.BoolVar(&options.Color, "color", terminal.IsTerminal(int(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb", "colored output")
74-
flagColor := pflag.Lookup("color")
75-
tags := pflag.String("tags", "", "a list of build tags to consider satisfied during the build")
76-
flagTags := pflag.Lookup("tags")
77-
78-
pflag.BoolVar(&options.MapToLocalDisk, "localmap", false, "use local paths for sourcemap")
79-
flagLocalMap := pflag.Lookup("localmap")
62+
var (
63+
options = &gbuild.Options{CreateMapFile: true}
64+
pkgObj string
65+
tags string
66+
)
67+
68+
flagVerbose := pflag.NewFlagSet("", 0)
69+
flagVerbose.BoolVarP(&options.Verbose, "verbose", "v", false, "print the names of packages as they are compiled")
70+
flagQuiet := pflag.NewFlagSet("", 0)
71+
flagQuiet.BoolVarP(&options.Quiet, "quiet", "q", false, "suppress non-fatal warnings")
72+
73+
compilerFlags := pflag.NewFlagSet("", 0)
74+
compilerFlags.BoolVarP(&options.Minify, "minify", "m", false, "minify generated code")
75+
compilerFlags.BoolVar(&options.Color, "color", terminal.IsTerminal(int(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb", "colored output")
76+
compilerFlags.StringVar(&tags, "tags", "", "a list of build tags to consider satisfied during the build")
77+
compilerFlags.BoolVar(&options.MapToLocalDisk, "localmap", false, "use local paths for sourcemap")
78+
79+
flagWatch := pflag.NewFlagSet("", 0)
80+
flagWatch.BoolVarP(&options.Watch, "watch", "w", false, "watch for changes to the source files")
8081

8182
cmdBuild := &cobra.Command{
8283
Use: "build [packages]",
8384
Short: "compile packages and dependencies",
8485
}
8586
cmdBuild.Flags().StringVarP(&pkgObj, "output", "o", "", "output file")
86-
cmdBuild.Flags().AddFlag(flagVerbose)
87-
cmdBuild.Flags().AddFlag(flagQuiet)
88-
cmdBuild.Flags().AddFlag(flagWatch)
89-
cmdBuild.Flags().AddFlag(flagMinify)
90-
cmdBuild.Flags().AddFlag(flagColor)
91-
cmdBuild.Flags().AddFlag(flagTags)
92-
cmdBuild.Flags().AddFlag(flagLocalMap)
87+
cmdBuild.Flags().AddFlagSet(flagVerbose)
88+
cmdBuild.Flags().AddFlagSet(flagQuiet)
89+
cmdBuild.Flags().AddFlagSet(compilerFlags)
90+
cmdBuild.Flags().AddFlagSet(flagWatch)
9391
cmdBuild.Run = func(cmd *cobra.Command, args []string) {
94-
options.BuildTags = strings.Fields(*tags)
92+
options.BuildTags = strings.Fields(tags)
9593
for {
9694
s := gbuild.NewSession(options)
9795

@@ -165,15 +163,12 @@ func main() {
165163
Use: "install [packages]",
166164
Short: "compile and install packages and dependencies",
167165
}
168-
cmdInstall.Flags().AddFlag(flagVerbose)
169-
cmdInstall.Flags().AddFlag(flagQuiet)
170-
cmdInstall.Flags().AddFlag(flagWatch)
171-
cmdInstall.Flags().AddFlag(flagMinify)
172-
cmdInstall.Flags().AddFlag(flagColor)
173-
cmdInstall.Flags().AddFlag(flagTags)
174-
cmdInstall.Flags().AddFlag(flagLocalMap)
166+
cmdInstall.Flags().AddFlagSet(flagVerbose)
167+
cmdInstall.Flags().AddFlagSet(flagQuiet)
168+
cmdInstall.Flags().AddFlagSet(compilerFlags)
169+
cmdInstall.Flags().AddFlagSet(flagWatch)
175170
cmdInstall.Run = func(cmd *cobra.Command, args []string) {
176-
options.BuildTags = strings.Fields(*tags)
171+
options.BuildTags = strings.Fields(tags)
177172
for {
178173
s := gbuild.NewSession(options)
179174

@@ -253,19 +248,18 @@ func main() {
253248
Use: "get [packages]",
254249
Short: "download and install packages and dependencies",
255250
}
256-
cmdGet.Flags().AddFlag(flagVerbose)
257-
cmdGet.Flags().AddFlag(flagQuiet)
258-
cmdGet.Flags().AddFlag(flagWatch)
259-
cmdGet.Flags().AddFlag(flagMinify)
260-
cmdGet.Flags().AddFlag(flagColor)
261-
cmdGet.Flags().AddFlag(flagTags)
262-
cmdGet.Flags().AddFlag(flagLocalMap)
251+
cmdGet.Flags().AddFlagSet(flagVerbose)
252+
cmdGet.Flags().AddFlagSet(flagQuiet)
253+
cmdGet.Flags().AddFlagSet(compilerFlags)
263254
cmdGet.Run = cmdInstall.Run
264255

265256
cmdRun := &cobra.Command{
266257
Use: "run [gofiles...] [arguments...]",
267258
Short: "compile and run Go program",
268259
}
260+
cmdRun.Flags().AddFlagSet(flagVerbose)
261+
cmdRun.Flags().AddFlagSet(flagQuiet)
262+
cmdRun.Flags().AddFlagSet(compilerFlags)
269263
cmdRun.Run = func(cmd *cobra.Command, args []string) {
270264
err := func() error {
271265
lastSourceArg := 0
@@ -316,12 +310,9 @@ func main() {
316310
verbose := cmdTest.Flags().BoolP("verbose", "v", false, "Log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds.")
317311
compileOnly := cmdTest.Flags().BoolP("compileonly", "c", false, "Compile the test binary to pkg.test.js but do not run it (where pkg is the last element of the package's import path). The file name can be changed with the -o flag.")
318312
outputFilename := cmdTest.Flags().StringP("output", "o", "", "Compile the test binary to the named file. The test still runs (unless -c is specified).")
319-
cmdTest.Flags().AddFlag(flagMinify)
320-
cmdTest.Flags().AddFlag(flagColor)
321-
cmdTest.Flags().AddFlag(flagTags)
322-
cmdTest.Flags().AddFlag(flagLocalMap)
313+
cmdTest.Flags().AddFlagSet(compilerFlags)
323314
cmdTest.Run = func(cmd *cobra.Command, args []string) {
324-
options.BuildTags = strings.Fields(*tags)
315+
options.BuildTags = strings.Fields(tags)
325316
err := func() error {
326317
pkgs := make([]*gbuild.PackageData, len(args))
327318
for i, pkgPath := range args {
@@ -507,16 +498,13 @@ func main() {
507498
Use: "serve [root]",
508499
Short: "compile on-the-fly and serve",
509500
}
510-
cmdServe.Flags().AddFlag(flagVerbose)
511-
cmdServe.Flags().AddFlag(flagQuiet)
512-
cmdServe.Flags().AddFlag(flagMinify)
513-
cmdServe.Flags().AddFlag(flagColor)
514-
cmdServe.Flags().AddFlag(flagTags)
515-
cmdServe.Flags().AddFlag(flagLocalMap)
501+
cmdServe.Flags().AddFlagSet(flagVerbose)
502+
cmdServe.Flags().AddFlagSet(flagQuiet)
503+
cmdServe.Flags().AddFlagSet(compilerFlags)
516504
var addr string
517505
cmdServe.Flags().StringVarP(&addr, "http", "", ":8080", "HTTP bind address to serve")
518506
cmdServe.Run = func(cmd *cobra.Command, args []string) {
519-
options.BuildTags = strings.Fields(*tags)
507+
options.BuildTags = strings.Fields(tags)
520508
dirs := append(filepath.SplitList(build.Default.GOPATH), build.Default.GOROOT)
521509
var root string
522510

0 commit comments

Comments
 (0)