// first argument must be a full package path. This is similar to the
// command-line usage for the godoc command.
//
+// For commands, unless the -cmd flag is present "go doc command"
+// shows only the package-level docs for the package.
+//
// For complete documentation, run "go help doc".
package main
var (
unexported bool // -u flag
matchCase bool // -c flag
+ showCmd bool // -cmd flag
)
// usage is a replacement usage function for the flags package.
matchCase = false
flagSet.BoolVar(&unexported, "u", false, "show unexported symbols as well as exported")
flagSet.BoolVar(&matchCase, "c", false, "symbol matching honors case (paths not affected)")
+ flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command")
flagSet.Parse(args)
buildPackage, userPath, symbol := parseArgs(flagSet.Args())
symbol, method := parseSymbol(symbol)
// packageDoc prints the docs for the package (package doc plus one-liners of the rest).
func (pkg *Package) packageDoc() {
defer pkg.flush()
- pkg.packageClause(false)
+ if pkg.showInternals() {
+ pkg.packageClause(false)
+ }
doc.ToText(&pkg.buf, pkg.doc.Doc, "", "\t", 80)
- pkg.newlines(2)
+ pkg.newlines(1)
+
+ if !pkg.showInternals() {
+ // Show only package docs for commands.
+ return
+ }
+ pkg.newlines(1)
pkg.valueSummary(pkg.doc.Consts)
pkg.valueSummary(pkg.doc.Vars)
pkg.funcSummary(pkg.doc.Funcs)
pkg.bugs()
}
+// showInternals reports whether we should show the internals
+// of a package as opposed to just the package docs.
+// Used to decide whether to suppress internals for commands.
+// Called only by Package.packageDoc.
+func (pkg *Package) showInternals() bool {
+ return pkg.pkg.Name != "main" || showCmd
+}
+
// packageClause prints the package clause.
// The argument boolean, if true, suppresses the output if the
// user's argument is identical to the actual package path or
go doc
-it prints the package documentation for the package in the current directory.
+it prints the package documentation for the package in the current directory. If
+the package is a command (package main), the exported symbols of the package are
+elided from the presentation unless the -cmd flag is provided.
When run with one argument, the argument is treated as a Go-syntax-like representation
of the item to be documented. What the argument selects depends on what is installed
Show documentation and method summary for json.Number.
go doc json.Number.Int64 (or go doc json.number.int64)
Show documentation for json.Number's Int64 method.
+ go doc cmd/doc
+ Show package docs for the doc command.
+ go doc -cmd cmd/doc
+ Show package docs and exported symbols within the doc command.
go doc template.new
Show documentation for html/template's New function.
(html/template is lexically before text/template)
Flags:
-c
Respect case when matching symbols.
+ -cmd
+ Treat a command (package main) like a regular package.
+ Otherwise package main's exported symbols are hidden
+ when showing the package's top-level documentation.
-u
Show documentation for unexported as well as exported
symbols and methods.
go doc
-it prints the package documentation for the package in the current directory.
+it prints the package documentation for the package in the current directory. If
+the package is a command (package main), the exported symbols of the package are
+elided from the presentation unless the -cmd flag is provided.
When run with one argument, the argument is treated as a Go-syntax-like representation
of the item to be documented. What the argument selects depends on what is installed
Show documentation and method summary for json.Number.
go doc json.Number.Int64 (or go doc json.number.int64)
Show documentation for json.Number's Int64 method.
+ go doc cmd/doc
+ Show package docs for the doc command.
+ go doc -cmd cmd/doc
+ Show package docs and exported symbols within the doc command.
go doc template.new
Show documentation for html/template's New function.
(html/template is lexically before text/template)
Flags:
-c
Respect case when matching symbols.
+ -cmd
+ Treat a command (package main) like a regular package.
+ Otherwise package main's exported symbols are hidden
+ when showing the package's top-level documentation.
-u
Show documentation for unexported as well as exported
symbols and methods.