-
Notifications
You must be signed in to change notification settings - Fork 3
/
help.go
73 lines (61 loc) · 1.34 KB
/
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
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 (
_ "embed"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"os"
"regexp"
"sort"
)
//go:embed README.mdwn
var readme []byte
// Help prints tool listing
func Help(args []string, _ io.Reader) error {
flags := flag.NewFlagSet("help", flag.ExitOnError)
var full bool
flags.BoolVar(&full, "v", false, "show full help")
var command string
flags.StringVar(&command, "command", "", "show help for just command")
err := flags.Parse(args[1:])
if err != nil {
return fmt.Errorf("flags.Parse: %w", err)
}
if full {
comment, err := regexp.Compile(`<!--.*?-->\n?`)
if err != nil {
return err
}
fmt.Print(string(comment.ReplaceAll(readme, []byte{})))
return nil
}
if command != "" {
doc, err := fs.ReadFile(helpFS, helpPaths[command])
if err != nil && errors.Is(err, fs.ErrNotExist) {
fmt.Fprintf(os.Stderr, "No such command: %s\n", command)
os.Exit(1)
}
if err != nil {
return err
}
fmt.Print(string(doc))
return nil
}
tools := make([]string, 0, len(Dispatch))
for k := range Dispatch {
if k == "xyzzy" { // nothing to see here
continue
}
tools = append(tools, k)
}
str := "Tools:\n"
sort.Strings(tools)
for _, k := range tools {
str += " * " + k + "\n"
}
str += "\nGet more help for each tool with `leatherman help -command <tool>`, or `leatherman help -v`"
fmt.Println(str)
return nil
}