Repository navigation
Expand file tree
/
Copy pathhelp.go
More file actions
73 lines (57 loc) · 1.3 KB
/
Copy pathhelp.go
File metadata and controls
73 lines (57 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"text/template"
)
var helpCommand = Command{
Name: "help",
Short: "get help for a command",
Usage: "help [command]",
Long: `
Help gets detailed usage information for a command.
`,
GetTask: NewHelpTask,
}
var helpTemplate = `
Usage: goenv {{.Usage}}
{{.Long | trim}}
`
// HelpTask initializes a goenv.
type HelpTask struct {
CommandName string // the name of the command.
Commands map[string]*Command // the map of commands.
}
// NewHelpTask returns a new HelpTask created with the specified command-line args.
func NewHelpTask(args []string) (Task, error) {
flags := flag.NewFlagSet("help", flag.ExitOnError)
flags.Parse(args)
args = flags.Args()
if len(args) < 1 {
return nil, errors.New("no command specified")
}
return &HelpTask{
CommandName: args[0],
Commands: commands,
}, nil
}
// Run exeuctes the HelpTask
func (task *HelpTask) Run() error {
cmd, found := task.Commands[task.CommandName]
if !found {
return fmt.Errorf("no such command \"%s\"", cmd)
}
tmpl := template.New("help")
tmpl.Funcs(template.FuncMap{
"trim": strings.TrimSpace,
})
tmpl, err := tmpl.Parse(strings.TrimSpace(helpTemplate) + "\n\n")
if err != nil {
return err
}
err = tmpl.Execute(os.Stdout, cmd)
return err
}