Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit c47b7d0

Browse files
committed
命令行参数变量采用 flagXXX 格式命名
1 parent 4169e2b commit c47b7d0

File tree

6 files changed

+78
-76
lines changed

6 files changed

+78
-76
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
appengine/goroot.zip
2+
golangdoc.exe

appinit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func init() {
3636

3737
corpus := godoc.NewCorpus(fs)
3838
corpus.Verbose = false
39-
corpus.MaxResults = 10000 // matches flag default in main.go
39+
corpus.MaxResults = 10000 // matches flag default in main.go
4040
corpus.IndexEnabled = true
4141
corpus.IndexFiles = indexFilenames
4242

blog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func init() {
3939
}
4040

4141
func blogInit() {
42-
blogFS := local.BlogFS(*lang)
42+
blogFS := local.BlogFS(*flagLang)
4343

4444
// If content is not available fall back to redirect.
4545
if fi, err := blogFS.Lstat("/"); err != nil || !fi.IsDir() {

main.go

+67-67
Original file line numberDiff line numberDiff line change
@@ -58,50 +58,50 @@ const (
5858
var (
5959
// file system to serve
6060
// (with e.g.: zip -r go.zip $GOROOT -i \*.go -i \*.html -i \*.css -i \*.js -i \*.txt -i \*.c -i \*.h -i \*.s -i \*.png -i \*.jpg -i \*.sh -i favicon.ico)
61-
zipfile = flag.String("zip", "", "zip file providing the file system to serve; disabled if empty")
61+
flagZipfile = flag.String("zip", "", "zip file providing the file system to serve; disabled if empty")
6262

6363
// file-based index
64-
writeIndex = flag.Bool("write_index", false, "write index to a file; the file name must be specified with -index_files")
64+
flagWriteIndex = flag.Bool("write_index", false, "write index to a file; the file name must be specified with -index_files")
6565

66-
analysisFlag = flag.String("analysis", "", `comma-separated list of analyses to perform (supported: type, pointer). See http://golang.org/lib/godoc/analysis/help.html`)
66+
flagAnalysisFlag = flag.String("analysis", "", `comma-separated list of analyses to perform (supported: type, pointer). See http://golang.org/lib/godoc/analysis/help.html`)
6767

6868
// network
69-
httpAddr = flag.String("http", "", "HTTP service address (e.g., '"+defaultAddr+"')")
70-
serverAddr = flag.String("server", "", "webserver address for command line searches")
69+
flagHttpAddr = flag.String("http", "", "HTTP service address (e.g., '"+defaultAddr+"')")
70+
flagServerAddr = flag.String("server", "", "webserver address for command line searches")
7171

7272
// layout control
73-
html = flag.Bool("html", false, "print HTML in command-line mode")
74-
srcMode = flag.Bool("src", false, "print (exported) source in command-line mode")
75-
urlFlag = flag.String("url", "", "print HTML for named URL")
73+
flagHtml = flag.Bool("html", false, "print HTML in command-line mode")
74+
flagSrcMode = flag.Bool("src", false, "print (exported) source in command-line mode")
75+
flagUrlFlag = flag.String("url", "", "print HTML for named URL")
7676

7777
// command-line searches
78-
query = flag.Bool("q", false, "arguments are considered search queries")
78+
flagQuery = flag.Bool("q", false, "arguments are considered search queries")
7979

80-
verbose = flag.Bool("v", false, "verbose mode")
80+
flagVerbose = flag.Bool("v", false, "verbose mode")
8181

8282
// file system roots
8383
// TODO(gri) consider the invariant that goroot always end in '/'
84-
goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory")
84+
flagGoroot = flag.String("goroot", runtime.GOROOT(), "Go root directory")
8585

8686
// layout control
87-
tabWidth = flag.Int("tabwidth", 4, "tab width")
88-
showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
89-
templateDir = flag.String("templates", "", "directory containing alternate template files")
90-
showPlayground = flag.Bool("play", false, "enable playground in web interface")
91-
showExamples = flag.Bool("ex", false, "show examples in command line mode")
92-
declLinks = flag.Bool("links", true, "link identifiers to their declarations")
87+
flagTabWidth = flag.Int("tabwidth", 4, "tab width")
88+
flagShowTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
89+
flagTemplateDir = flag.String("templates", "", "directory containing alternate template files")
90+
flagShowPlayground = flag.Bool("play", false, "enable playground in web interface")
91+
flagShowExamples = flag.Bool("ex", false, "show examples in command line mode")
92+
flagDeclLinks = flag.Bool("links", true, "link identifiers to their declarations")
9393

9494
// search index
95-
indexEnabled = flag.Bool("index", false, "enable search index")
96-
indexFiles = flag.String("index_files", "", "glob pattern specifying index files; if not empty, the index is read from these files in sorted order")
97-
maxResults = flag.Int("maxresults", 10000, "maximum number of full text search results shown")
98-
indexThrottle = flag.Float64("index_throttle", 0.75, "index throttle value; 0.0 = no time allocated, 1.0 = full throttle")
95+
flagIndexEnabled = flag.Bool("index", false, "enable search index")
96+
flagIndexFiles = flag.String("index_files", "", "glob pattern specifying index files; if not empty, the index is read from these files in sorted order")
97+
flagMaxResults = flag.Int("maxresults", 10000, "maximum number of full text search results shown")
98+
flagIndexThrottle = flag.Float64("index_throttle", 0.75, "index throttle value; 0.0 = no time allocated, 1.0 = full throttle")
9999

100100
// source code notes
101-
notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
101+
flagNotesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
102102

103103
// local language
104-
lang = flag.String("lang", "", "local language")
104+
flagLang = flag.String("lang", "", "local language")
105105
)
106106

107107
func usage() {
@@ -121,7 +121,7 @@ func loggingHandler(h http.Handler) http.Handler {
121121

122122
func handleURLFlag() {
123123
// Try up to 10 fetches, following redirects.
124-
urlstr := *urlFlag
124+
urlstr := *flagUrlFlag
125125
for i := 0; i < 10; i++ {
126126
// Prepare request.
127127
u, err := url.Parse(urlstr)
@@ -157,16 +157,16 @@ func handleURLFlag() {
157157

158158
func runGodoc() {
159159
// Determine file system to use.
160-
local.Init(*goroot, *zipfile, *templateDir, build.Default.GOPATH)
160+
local.Init(*flagGoroot, *flagZipfile, *flagTemplateDir, build.Default.GOPATH)
161161
fs.Bind("/", local.RootFS(), "/", vfs.BindReplace)
162-
fs.Bind("/lib/godoc", local.StaticFS(*lang), "/", vfs.BindReplace)
163-
fs.Bind("/doc", local.DocumentFS(*lang), "/", vfs.BindReplace)
162+
fs.Bind("/lib/godoc", local.StaticFS(*flagLang), "/", vfs.BindReplace)
163+
fs.Bind("/doc", local.DocumentFS(*flagLang), "/", vfs.BindReplace)
164164

165-
httpMode := *httpAddr != ""
165+
httpMode := *flagHttpAddr != ""
166166

167167
var typeAnalysis, pointerAnalysis bool
168-
if *analysisFlag != "" {
169-
for _, a := range strings.Split(*analysisFlag, ",") {
168+
if *flagAnalysisFlag != "" {
169+
for _, a := range strings.Split(*flagAnalysisFlag, ",") {
170170
switch a {
171171
case "type":
172172
typeAnalysis = true
@@ -182,62 +182,62 @@ func runGodoc() {
182182

183183
// translate hook
184184
corpus.SummarizePackage = func(importPath string) (summary string, showList, ok bool) {
185-
if pkg := local.Package(*lang, importPath); pkg != nil {
185+
if pkg := local.Package(*flagLang, importPath); pkg != nil {
186186
summary = doc.Synopsis(pkg.Doc)
187187
}
188188
ok = (summary != "")
189189
return
190190
}
191191
corpus.TranslateDocPackage = func(pkg *doc.Package) *doc.Package {
192-
return local.Package(*lang, pkg.ImportPath, pkg)
192+
return local.Package(*flagLang, pkg.ImportPath, pkg)
193193
}
194194

195-
corpus.Verbose = *verbose
196-
corpus.MaxResults = *maxResults
197-
corpus.IndexEnabled = *indexEnabled && httpMode
198-
if *maxResults == 0 {
195+
corpus.Verbose = *flagVerbose
196+
corpus.MaxResults = *flagMaxResults
197+
corpus.IndexEnabled = *flagIndexEnabled && httpMode
198+
if *flagMaxResults == 0 {
199199
corpus.IndexFullText = false
200200
}
201-
corpus.IndexFiles = *indexFiles
202-
corpus.IndexThrottle = *indexThrottle
203-
if *writeIndex {
201+
corpus.IndexFiles = *flagIndexFiles
202+
corpus.IndexThrottle = *flagIndexThrottle
203+
if *flagWriteIndex {
204204
corpus.IndexThrottle = 1.0
205205
corpus.IndexEnabled = true
206206
}
207-
if *writeIndex || httpMode || *urlFlag != "" {
207+
if *flagWriteIndex || httpMode || *flagUrlFlag != "" {
208208
if err := corpus.Init(); err != nil {
209209
log.Fatal(err)
210210
}
211211
}
212212

213213
pres = godoc.NewPresentation(corpus)
214-
pres.TabWidth = *tabWidth
215-
pres.ShowTimestamps = *showTimestamps
216-
pres.ShowPlayground = *showPlayground
217-
pres.ShowExamples = *showExamples
218-
pres.DeclLinks = *declLinks
219-
pres.SrcMode = *srcMode
220-
pres.HTMLMode = *html
221-
if *notesRx != "" {
222-
pres.NotesRx = regexp.MustCompile(*notesRx)
214+
pres.TabWidth = *flagTabWidth
215+
pres.ShowTimestamps = *flagShowTimestamps
216+
pres.ShowPlayground = *flagShowPlayground
217+
pres.ShowExamples = *flagShowExamples
218+
pres.DeclLinks = *flagDeclLinks
219+
pres.SrcMode = *flagSrcMode
220+
pres.HTMLMode = *flagHtml
221+
if *flagNotesRx != "" {
222+
pres.NotesRx = regexp.MustCompile(*flagNotesRx)
223223
}
224224

225-
readTemplates(pres, httpMode || *urlFlag != "")
225+
readTemplates(pres, httpMode || *flagUrlFlag != "")
226226
registerHandlers(pres)
227227

228-
if *writeIndex {
228+
if *flagWriteIndex {
229229
// Write search index and exit.
230-
if *indexFiles == "" {
230+
if *flagIndexFiles == "" {
231231
log.Fatal("no index file specified")
232232
}
233233

234234
log.Println("initialize file systems")
235-
*verbose = true // want to see what happens
235+
*flagVerbose = true // want to see what happens
236236

237237
corpus.UpdateIndex()
238238

239-
log.Println("writing index file", *indexFiles)
240-
f, err := os.Create(*indexFiles)
239+
log.Println("writing index file", *flagIndexFiles)
240+
f, err := os.Create(*flagIndexFiles)
241241
if err != nil {
242242
log.Fatal(err)
243243
}
@@ -252,33 +252,33 @@ func runGodoc() {
252252
}
253253

254254
// Print content that would be served at the URL *urlFlag.
255-
if *urlFlag != "" {
255+
if *flagUrlFlag != "" {
256256
handleURLFlag()
257257
return
258258
}
259259

260260
if httpMode {
261261
// HTTP server mode.
262262
var handler http.Handler = http.DefaultServeMux
263-
if *verbose {
263+
if *flagVerbose {
264264
log.Printf("Go Documentation Server")
265265
log.Printf("version = %s", runtime.Version())
266-
log.Printf("address = %s", *httpAddr)
267-
log.Printf("goroot = %s", *goroot)
268-
log.Printf("tabwidth = %d", *tabWidth)
266+
log.Printf("address = %s", *flagHttpAddr)
267+
log.Printf("goroot = %s", *flagGoroot)
268+
log.Printf("tabwidth = %d", *flagTabWidth)
269269
switch {
270-
case !*indexEnabled:
270+
case !*flagIndexEnabled:
271271
log.Print("search index disabled")
272-
case *maxResults > 0:
273-
log.Printf("full text index enabled (maxresults = %d)", *maxResults)
272+
case *flagMaxResults > 0:
273+
log.Printf("full text index enabled (maxresults = %d)", *flagMaxResults)
274274
default:
275275
log.Print("identifier search index enabled")
276276
}
277277
handler = loggingHandler(handler)
278278
}
279279

280280
// Initialize search index.
281-
if *indexEnabled {
281+
if *flagIndexEnabled {
282282
go corpus.RunIndexer()
283283
}
284284

@@ -288,14 +288,14 @@ func runGodoc() {
288288
}
289289

290290
// Start http server.
291-
if err := http.ListenAndServe(*httpAddr, handler); err != nil {
292-
log.Fatalf("ListenAndServe %s: %v", *httpAddr, err)
291+
if err := http.ListenAndServe(*flagHttpAddr, handler); err != nil {
292+
log.Fatalf("ListenAndServe %s: %v", *flagHttpAddr, err)
293293
}
294294

295295
return
296296
}
297297

298-
if *query {
298+
if *flagQuery {
299299
handleRemoteSearch()
300300
return
301301
}

main_windows.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func main() {
4646
flag.Usage = usage
4747
flag.Parse()
4848

49-
playEnabled = *showPlayground
49+
playEnabled = *flagShowPlayground
5050

5151
if *flagServiceInstall {
5252
var args []string
53-
args = append(args, fmt.Sprintf("-goroot=%s", *goroot))
53+
args = append(args, fmt.Sprintf("-goroot=%s", *flagGoroot))
5454
for i := 1; i < len(os.Args); i++ {
5555
if strings.HasPrefix(os.Args[i], "-service-install") {
5656
continue
@@ -60,7 +60,7 @@ func main() {
6060
}
6161
args = append(args, os.Args[i])
6262
}
63-
if *httpAddr == "" {
63+
if *flagHttpAddr == "" {
6464
args = append(args, "-http=:6060")
6565
}
6666
if err := installService(ServiceName, ServiceDesc, args...); err != nil {
@@ -92,7 +92,7 @@ func main() {
9292
}
9393

9494
// Check usage: either server and no args, command line and args, or index creation mode
95-
if (*httpAddr != "" || *urlFlag != "") != (flag.NArg() == 0) && !*writeIndex {
95+
if (*flagHttpAddr != "" || *flagUrlFlag != "") != (flag.NArg() == 0) && !*flagWriteIndex {
9696
usage()
9797
}
9898

remotesearch.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func remoteSearchURL(query string, html bool) string {
4444
func remoteSearch(query string) (res *http.Response, err error) {
4545
// list of addresses to try
4646
var addrs []string
47-
if *serverAddr != "" {
47+
if *flagServerAddr != "" {
4848
// explicit server address - only try this one
49-
addrs = []string{*serverAddr}
49+
addrs = []string{*flagServerAddr}
5050
} else {
5151
addrs = []string{
5252
defaultAddr,
@@ -55,7 +55,7 @@ func remoteSearch(query string) (res *http.Response, err error) {
5555
}
5656

5757
// remote search
58-
search := remoteSearchURL(query, *html)
58+
search := remoteSearchURL(query, *flagHtml)
5959
for _, addr := range addrs {
6060
url := "http://" + addr + search
6161
res, err = http.Get(url)

0 commit comments

Comments
 (0)