Commit 101a677e authored by Rob Pike's avatar Rob Pike

cmd/doc: add -all flag to print all documentation for package

Unlike the one for the old godoc, you need the -u flag to see
unexported symbols. This seems like the right behavior: it's
consistent.

For now at least, the argument must be a package, not a symbol.
This is also different from old godoc.

Required a little refactoring but also cleaned up a few things.

Update #25595

Leaving the bug open for now until we tackle
	go doc -all symbol

Change-Id: Ibc1975bfa592cb1e92513eb2e5e9e11e01a60095
Reviewed-on: https://go-review.googlesource.com/c/141977
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent ee769922
......@@ -147,6 +147,69 @@ var tests = []test{
`type T1 T2`, // Type alias does not display as type declaration.
},
},
// Package dump -all
{
"full package",
[]string{"-all", p},
[]string{
`package pkg .*import`,
`Package comment`,
`CONSTANTS`,
`Comment before ConstOne`,
`ConstOne = 1`,
`ConstTwo = 2 // Comment on line with ConstTwo`,
`ConstFive`,
`ConstSix`,
`Const block where first entry is unexported`,
`ConstLeft2, constRight2 uint64`,
`constLeft3, ConstRight3`,
`ConstLeft4, ConstRight4`,
`Duplicate = iota`,
`const CaseMatch = 1`,
`const Casematch = 2`,
`const ExportedConstant = 1`,
`const MultiLineConst = `,
`MultiLineString1`,
`VARIABLES`,
`Comment before VarOne`,
`VarOne = 1`,
`Comment about block of variables`,
`VarFive = 5`,
`var ExportedVariable = 1`,
`var LongLine = newLongLine\(`,
`var MultiLineVar = map\[struct {`,
`FUNCTIONS`,
`func ExportedFunc\(a int\) bool`,
`Comment about exported function`,
`func MultiLineFunc\(x interface`,
`func ReturnUnexported\(\) unexportedType`,
`TYPES`,
`type ExportedInterface interface`,
`type ExportedStructOneField struct`,
`type ExportedType struct`,
`Comment about exported type`,
`const ConstGroup4 ExportedType = ExportedType`,
`ExportedTypedConstant ExportedType = iota`,
`Constants tied to ExportedType`,
`func ExportedTypeConstructor\(\) \*ExportedType`,
`Comment about constructor for exported type`,
`func ReturnExported\(\) ExportedType`,
`func \(ExportedType\) ExportedMethod\(a int\) bool`,
`Comment about exported method`,
`type T1 = T2`,
`type T2 int`,
},
[]string{
`constThree`,
`_, _ uint64 = 2 \* iota, 1 << iota`,
`constLeft1, constRight1`,
`duplicate`,
`varFour`,
`func internalFunc`,
`unexportedField`,
`func \(unexportedType\)`,
},
},
// Package dump -u
{
"full package with u",
......@@ -164,6 +227,58 @@ var tests = []test{
`MultiLine(String|Method|Field)`, // No data from multi line portions.
},
},
// Package dump -u -all
{
"full package",
[]string{"-u", "-all", p},
[]string{
`package pkg .*import`,
`Package comment`,
`CONSTANTS`,
`Comment before ConstOne`,
`ConstOne += 1`,
`ConstTwo += 2 // Comment on line with ConstTwo`,
`constThree = 3 // Comment on line with constThree`,
`ConstFive`,
`const internalConstant += 2`,
`Comment about internal constant`,
`VARIABLES`,
`Comment before VarOne`,
`VarOne += 1`,
`Comment about block of variables`,
`varFour += 4`,
`VarFive += 5`,
`varSix += 6`,
`var ExportedVariable = 1`,
`var LongLine = newLongLine\(`,
`var MultiLineVar = map\[struct {`,
`var internalVariable = 2`,
`Comment about internal variable`,
`FUNCTIONS`,
`func ExportedFunc\(a int\) bool`,
`Comment about exported function`,
`func MultiLineFunc\(x interface`,
`func internalFunc\(a int\) bool`,
`Comment about internal function`,
`func newLongLine\(ss .*string\)`,
`TYPES`,
`type ExportedType struct`,
`type T1 = T2`,
`type T2 int`,
`type unexportedType int`,
`Comment about unexported type`,
`ConstGroup1 unexportedType = iota`,
`ConstGroup2`,
`ConstGroup3`,
`ExportedTypedConstant_unexported unexportedType = iota`,
`Constants tied to unexportedType`,
`const unexportedTypedConstant unexportedType = 1`,
`func ReturnUnexported\(\) unexportedType`,
`func \(unexportedType\) ExportedMethod\(\) bool`,
`func \(unexportedType\) unexportedMethod\(\) bool`,
},
nil,
},
// Single constant.
{
......@@ -361,7 +476,6 @@ var tests = []test{
`io.Reader.*Comment on line with embedded Reader`,
},
[]string{
`int.*embedded`, // No unexported embedded field.
`Comment about exported method`, // No comment about exported method.
`unexportedMethod`, // No unexported method.
`unexportedTypedConstant`, // No unexported constant.
......
......@@ -31,6 +31,9 @@
// The -src flag causes doc to print the full source code for the symbol, such
// as the body of a struct, function or method.
//
// The -all flag causes doc to print all documentation for the package and
// all its visible symbols. The argument must identify a package.
//
// For complete documentation, run "go help doc".
package main
......@@ -52,6 +55,7 @@ import (
var (
unexported bool // -u flag
matchCase bool // -c flag
showAll bool // -all flag
showCmd bool // -cmd flag
showSrc bool // -src flag
)
......@@ -88,6 +92,7 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
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(&showAll, "all", false, "show all documentation for package")
flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command")
flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol")
flagSet.Parse(args)
......@@ -127,6 +132,15 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
unexported = true
}
// We have a package.
if showAll {
if symbol != "" {
return fmt.Errorf("-all valid only for package, not symbol: %s", symbol)
}
pkg.allDoc()
return
}
switch {
case symbol == "":
pkg.packageDoc() // The package exists, so we got some output.
......
This diff is collapsed.
......@@ -342,6 +342,8 @@
// cd go/src/encoding/json; go doc decode
//
// Flags:
// -all
// Show all the documentation for the package.
// -c
// Respect case when matching symbols.
// -cmd
......
......@@ -106,6 +106,8 @@ Examples:
cd go/src/encoding/json; go doc decode
Flags:
-all
Show all the documentation for the package.
-c
Respect case when matching symbols.
-cmd
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment