diff options
Diffstat (limited to 'libgo/go/cmd/go/internal/help/help.go')
-rw-r--r-- | libgo/go/cmd/go/internal/help/help.go | 87 |
1 files changed, 50 insertions, 37 deletions
diff --git a/libgo/go/cmd/go/internal/help/help.go b/libgo/go/cmd/go/internal/help/help.go index b4c5217f836..a80afe36c41 100644 --- a/libgo/go/cmd/go/internal/help/help.go +++ b/libgo/go/cmd/go/internal/help/help.go @@ -21,82 +21,95 @@ import ( // Help implements the 'help' command. func Help(args []string) { - if len(args) == 0 { - PrintUsage(os.Stdout) - // not exit 2: succeeded at 'go help'. - return - } - if len(args) != 1 { - fmt.Fprintf(os.Stderr, "usage: go help command\n\nToo many arguments given.\n") - os.Exit(2) // failed at 'go help' - } - - arg := args[0] - // 'go help documentation' generates doc.go. - if arg == "documentation" { + if len(args) == 1 && args[0] == "documentation" { fmt.Println("// Copyright 2011 The Go Authors. All rights reserved.") fmt.Println("// Use of this source code is governed by a BSD-style") fmt.Println("// license that can be found in the LICENSE file.") fmt.Println() - fmt.Println("// DO NOT EDIT THIS FILE. GENERATED BY mkalldocs.sh.") + fmt.Println("// Code generated by mkalldocs.sh; DO NOT EDIT.") fmt.Println("// Edit the documentation in other files and rerun mkalldocs.sh to generate this one.") fmt.Println() buf := new(bytes.Buffer) - PrintUsage(buf) + PrintUsage(buf, base.Go) usage := &base.Command{Long: buf.String()} - tmpl(&commentWriter{W: os.Stdout}, documentationTemplate, append([]*base.Command{usage}, base.Commands...)) + cmds := []*base.Command{usage} + for _, cmd := range base.Go.Commands { + if cmd.UsageLine == "gopath-get" { + // Avoid duplication of the "get" documentation. + continue + } + cmds = append(cmds, cmd) + cmds = append(cmds, cmd.Commands...) + } + tmpl(&commentWriter{W: os.Stdout}, documentationTemplate, cmds) fmt.Println("package main") return } - for _, cmd := range base.Commands { - if cmd.Name() == arg { - tmpl(os.Stdout, helpTemplate, cmd) - // not exit 2: succeeded at 'go help cmd'. - return + cmd := base.Go +Args: + for i, arg := range args { + for _, sub := range cmd.Commands { + if sub.Name() == arg { + cmd = sub + continue Args + } + } + + // helpSuccess is the help command using as many args as possible that would succeed. + helpSuccess := "go help" + if i > 0 { + helpSuccess = " " + strings.Join(args[:i], " ") } + fmt.Fprintf(os.Stderr, "go help %s: unknown help topic. Run '%s'.\n", strings.Join(args, " "), helpSuccess) + os.Exit(2) // failed at 'go help cmd' } - fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'go help'.\n", arg) - os.Exit(2) // failed at 'go help cmd' + if len(cmd.Commands) > 0 { + PrintUsage(os.Stdout, cmd) + } else { + tmpl(os.Stdout, helpTemplate, cmd) + } + // not exit 2: succeeded at 'go help cmd'. + return } -var usageTemplate = `Go is a tool for managing Go source code. +var usageTemplate = `{{.Long | trim}} Usage: - go command [arguments] + {{.UsageLine}} <command> [arguments] The commands are: -{{range .}}{{if .Runnable}} +{{range .Commands}}{{if or (.Runnable) .Commands}} {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}} -Use "go help [command]" for more information about a command. - +Use "go help{{with .LongName}} {{.}}{{end}} <command>" for more information about a command. +{{if eq (.UsageLine) "go"}} Additional help topics: -{{range .}}{{if not .Runnable}} +{{range .Commands}}{{if and (not .Runnable) (not .Commands)}} {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}} -Use "go help [topic]" for more information about that topic. - +Use "go help{{with .LongName}} {{.}}{{end}} <topic>" for more information about that topic. +{{end}} ` -var helpTemplate = `{{if .Runnable}}usage: go {{.UsageLine}} +var helpTemplate = `{{if .Runnable}}usage: {{.UsageLine}} {{end}}{{.Long | trim}} ` var documentationTemplate = `{{range .}}{{if .Short}}{{.Short | capitalize}} -{{end}}{{if .Runnable}}Usage: +{{end}}{{if .Commands}}` + usageTemplate + `{{else}}{{if .Runnable}}Usage: - go {{.UsageLine}} + {{.UsageLine}} {{end}}{{.Long | trim}} -{{end}}` +{{end}}{{end}}` // commentWriter writes a Go comment to the underlying io.Writer, // using line comment form (//). @@ -171,8 +184,8 @@ func capitalize(s string) string { return string(unicode.ToTitle(r)) + s[n:] } -func PrintUsage(w io.Writer) { +func PrintUsage(w io.Writer, cmd *base.Command) { bw := bufio.NewWriter(w) - tmpl(bw, usageTemplate, base.Commands) + tmpl(bw, usageTemplate, cmd) bw.Flush() } |