-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelp.go
40 lines (32 loc) · 811 Bytes
/
help.go
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
package comandante
import (
"fmt"
"io"
"os"
)
func createHelpCommand(com *Comandante, w io.Writer) *Command {
action := func() error {
// The first parameter passed to the help command should be the
// command for requested documentation.
if len(os.Args) < 3 {
com.printDefaultHelp(w)
} else {
cmdName := os.Args[2]
cmd := com.getCommand(cmdName)
if cmd != nil && cmdName != "help" {
fmt.Fprintf(w, "%s %s\n%s", com.binaryName, cmdName, cmd.Documentation)
if cmd.FlagInit != nil {
cmd.FlagInit(&cmd.flagSet)
fmt.Fprintf(w, "\noptions\n")
cmd.flagSet.SetOutput(w)
cmd.flagSet.PrintDefaults()
}
} else {
com.printDefaultHelp(w)
}
}
return nil
}
cmd := NewCommand("help", "get more information about a command", action)
return cmd
}