]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: invoke godoc with import path when possible
authorRuss Cox <rsc@golang.org>
Tue, 15 May 2012 16:53:57 +0000 (12:53 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 15 May 2012 16:53:57 +0000 (12:53 -0400)
Also add -n -x flags to doc, fmt, vet.
Also shorten unknown command error.

Fixes #3612.
Fixes #3613.

R=golang-dev, bradfitz, r
CC=golang-dev
https://golang.org/cl/6211053

src/cmd/go/build.go
src/cmd/go/doc.go
src/cmd/go/fmt.go
src/cmd/go/main.go
src/cmd/go/vet.go

index 2b9995d1c6c2d4af08490dfd45d68427e9f9f2f4..a68696c00d966b2b3ac08a4f53d4bbab94489e96 100644 (file)
@@ -152,6 +152,11 @@ func addBuildFlags(cmd *Command) {
        cmd.Flag.Var(buildCompiler{}, "compiler", "")
 }
 
+func addBuildFlagsNX(cmd *Command) {
+       cmd.Flag.BoolVar(&buildN, "n", false, "")
+       cmd.Flag.BoolVar(&buildX, "x", false, "")
+}
+
 type stringsFlag []string
 
 func (v *stringsFlag) Set(s string) error {
index 32ede39649deaa68c776a99c9b23c4d4f2193d75..a39534a99671f793e29d036bd6b9914b1e0a4685 100644 (file)
@@ -145,7 +145,7 @@ Run godoc on package sources
 
 Usage:
 
-       go doc [packages]
+       go doc [-n] [-x] [packages]
 
 Doc runs the godoc command on the packages named by the
 import paths.
@@ -153,6 +153,9 @@ import paths.
 For more about godoc, see 'godoc godoc'.
 For more about specifying packages, see 'go help packages'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 To run godoc with specific options, run godoc itself.
 
 See also: go fix, go fmt, go vet.
@@ -192,7 +195,7 @@ Run gofmt on package sources
 
 Usage:
 
-       go fmt [packages]
+       go fmt [-n] [-x] [packages]
 
 Fmt runs the command 'gofmt -l -w' on the packages named
 by the import paths.  It prints the names of the files that are modified.
@@ -200,6 +203,9 @@ by the import paths.  It prints the names of the files that are modified.
 For more about gofmt, see 'godoc gofmt'.
 For more about specifying packages, see 'go help packages'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 To run gofmt with specific options, run gofmt itself.
 
 See also: go doc, go fix, go vet.
@@ -414,7 +420,7 @@ Run go tool vet on packages
 
 Usage:
 
-       go vet [packages]
+       go vet [-n] [-x] [packages]
 
 Vet runs the Go vet command on the packages named by the import paths.
 
@@ -423,6 +429,9 @@ For more about specifying packages, see 'go help packages'.
 
 To run the vet tool with specific options, run 'go tool vet'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 See also: go fmt, go fix.
 
 
index cea9b0a512920215277a9bed6ac6e94fa028086e..b1aba32f3fb3bfe05f17f6ce9eb91669708d018e 100644 (file)
@@ -4,9 +4,14 @@
 
 package main
 
+func init() {
+       addBuildFlagsNX(cmdFmt)
+       addBuildFlagsNX(cmdDoc)
+}
+
 var cmdFmt = &Command{
        Run:       runFmt,
-       UsageLine: "fmt [packages]",
+       UsageLine: "fmt [-n] [-x] [packages]",
        Short:     "run gofmt on package sources",
        Long: `
 Fmt runs the command 'gofmt -l -w' on the packages named
@@ -15,6 +20,9 @@ by the import paths.  It prints the names of the files that are modified.
 For more about gofmt, see 'godoc gofmt'.
 For more about specifying packages, see 'go help packages'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 To run gofmt with specific options, run gofmt itself.
 
 See also: go doc, go fix, go vet.
@@ -32,7 +40,7 @@ func runFmt(cmd *Command, args []string) {
 
 var cmdDoc = &Command{
        Run:       runDoc,
-       UsageLine: "doc [packages]",
+       UsageLine: "doc [-n] [-x] [packages]",
        Short:     "run godoc on package sources",
        Long: `
 Doc runs the godoc command on the packages named by the
@@ -41,6 +49,9 @@ import paths.
 For more about godoc, see 'godoc godoc'.
 For more about specifying packages, see 'go help packages'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 To run godoc with specific options, run godoc itself.
 
 See also: go fix, go fmt, go vet.
@@ -53,6 +64,10 @@ func runDoc(cmd *Command, args []string) {
                        errorf("go doc: cannot use package file list")
                        continue
                }
-               run("godoc", pkg.Dir)
+               if pkg.local {
+                       run("godoc", pkg.Dir)
+               } else {
+                       run("godoc", pkg.ImportPath)
+               }
        }
 }
index 93a412428800c3f1b2ebdbcdc7598be524805fcd..a17082c2b41b43adab37af1ea57592638a2f5827 100644 (file)
@@ -144,8 +144,9 @@ func main() {
                }
        }
 
-       fmt.Fprintf(os.Stderr, "Unknown command %#q\n\n", args[0])
-       usage()
+       fmt.Fprintf(os.Stderr, "go: unknown subcommand %#q\nRun 'go help' for usage.\n", args[0])
+       setExitStatus(2)
+       exit()
 }
 
 var usageTemplate = `Go is a tool for managing Go source code.
@@ -339,6 +340,13 @@ func exitIfErrors() {
 
 func run(cmdargs ...interface{}) {
        cmdline := stringList(cmdargs...)
+       if buildN || buildV {
+               fmt.Printf("%s\n", strings.Join(cmdline, " "))
+               if buildN {
+                       return
+               }
+       }
+
        cmd := exec.Command(cmdline[0], cmdline[1:]...)
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
index a672b99108ed26c5ab543dc7b984dd0292038e0f..eb0b89ccadf8ba8892b91e8cc2b39fc42ba38d8b 100644 (file)
@@ -4,9 +4,13 @@
 
 package main
 
+func init() {
+       addBuildFlagsNX(cmdVet)
+}
+
 var cmdVet = &Command{
        Run:       runVet,
-       UsageLine: "vet [packages]",
+       UsageLine: "vet [-n] [-x] [packages]",
        Short:     "run go tool vet on packages",
        Long: `
 Vet runs the Go vet command on the packages named by the import paths.
@@ -16,6 +20,9 @@ For more about specifying packages, see 'go help packages'.
 
 To run the vet tool with specific options, run 'go tool vet'.
 
+The -n flag prints commands that would be executed.
+The -x flag prints commands as they are executed.
+
 See also: go fmt, go fix.
        `,
 }