cmd/godoc: provide -all flag to output unexported identifiers

This flag includes unexported identifiers in command-line mode.
It is equivalent to ?m=all in web mode.

Fixes golang/go#8093

Change-Id: I1e5a69626929d3430638d900f3e975b272a98c90
Reviewed-on: https://go-review.googlesource.com/99435
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go
index 901e992..aa2996a 100644
--- a/cmd/godoc/main.go
+++ b/cmd/godoc/main.go
@@ -72,6 +72,7 @@
 	// layout control
 	html    = flag.Bool("html", false, "print HTML in command-line mode")
 	srcMode = flag.Bool("src", false, "print (exported) source in command-line mode")
+	allMode = flag.Bool("all", false, "include unexported identifiers in command-line mode")
 	urlFlag = flag.String("url", "", "print HTML for named URL")
 
 	// command-line searches
@@ -253,6 +254,7 @@
 	pres.DeclLinks = *declLinks
 	pres.SrcMode = *srcMode
 	pres.HTMLMode = *html
+	pres.AllMode = *allMode
 	if *notesRx != "" {
 		pres.NotesRx = regexp.MustCompile(*notesRx)
 	}
diff --git a/godoc/cmdline.go b/godoc/cmdline.go
index 7c53681..b531b4d 100644
--- a/godoc/cmdline.go
+++ b/godoc/cmdline.go
@@ -48,6 +48,9 @@
 		// the fake built-in package contains unexported identifiers
 		mode = NoFiltering | NoTypeAssoc
 	}
+	if pres.AllMode {
+		mode |= NoFiltering
+	}
 	if srcMode {
 		// only filter exports if we don't have explicit command-line filter arguments
 		if len(args) > 1 {
diff --git a/godoc/cmdline_test.go b/godoc/cmdline_test.go
index 602f2bb..8c9ce22 100644
--- a/godoc/cmdline_test.go
+++ b/godoc/cmdline_test.go
@@ -172,6 +172,10 @@
 // Second function is second.
 func Second() {
 }
+
+// unexported function is third.
+func unexported() {
+}
 `,
 		"src/gen/gen.go": `// Package gen
 package gen
@@ -220,6 +224,7 @@
 	for _, tc := range []struct {
 		desc string
 		args []string
+		all  bool
 		exp  string
 		err  bool
 	}{
@@ -254,6 +259,18 @@
 			exp:  "// Second function is second.\nfunc Second() {\n}",
 		},
 		{
+			desc: "package w. unexported filter",
+			args: []string{"foo", "unexported"},
+			all:  true,
+			exp:  "PACKAGE \nfunc unexported()\n    unexported function is third.\n",
+		},
+		{
+			desc: "package w. unexported filter",
+			args: []string{"foo", "unexported"},
+			all:  false,
+			exp:  "PACKAGE ",
+		},
+		{
 			desc: "package w. //line comments",
 			args: []string{"gen", "F"},
 			exp:  "PACKAGE \nfunc F()\n    F doc //line 1 should appear line 2 should appear\n",
@@ -284,11 +301,12 @@
 			exp:  "",
 		},
 	} {
+		p.AllMode = tc.all
 		w := new(bytes.Buffer)
 		err := CommandLine(w, fs, p, tc.args)
 		if got, want := w.String(), tc.exp; got != want || tc.err == (err == nil) {
-			t.Errorf("%s: CommandLine(%v) = %q (%v); want %q (%v)",
-				tc.desc, tc.args, got, err, want, tc.err)
+			t.Errorf("%s: CommandLine(%v), All(%v) = %q (%v); want %q (%v)",
+				tc.desc, tc.args, tc.all, got, err, want, tc.err)
 		}
 	}
 }
diff --git a/godoc/pres.go b/godoc/pres.go
index 8551177..b8205e2 100644
--- a/godoc/pres.go
+++ b/godoc/pres.go
@@ -53,6 +53,8 @@
 	SrcMode bool
 	// HTMLMode outputs HTML instead of plain text in command-line mode.
 	HTMLMode bool
+	// AllMode includes unexported identifiers in the output in command-line mode.
+	AllMode bool
 
 	// NotesRx optionally specifies a regexp to match
 	// notes to render in the output.