-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelp.go
105 lines (84 loc) · 2.05 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
_ "embed"
"flag"
"fmt"
"io"
"sort"
"strings"
"braces.dev/errtrace"
)
// Help is doc2go's -h/-help flag.
// It supports retrieving help on various topics by passing in a parameter.
type Help string
var (
//go:embed help/default.txt
_defaultHelp string
//go:embed help/frontmatter.txt
_frontmatterHelp string
//go:embed help/pkg-doc.txt
_packageDocHelp string
//go:embed help/highlight.txt
_highlightHelp string
//go:embed help/config.txt
_configHelp string
//go:embed help/pagefind.txt
_pagefindHelp string
_usageHelp = firstLineOf(_defaultHelp)
_helpTopics = map[Help]string{
"config": _configHelp,
"default": _defaultHelp,
"frontmatter": _frontmatterHelp,
"highlight": _highlightHelp,
"pagefind": _pagefindHelp,
"pkg-doc": _packageDocHelp,
"usage": _usageHelp,
}
)
func firstLineOf(s string) string {
if idx := strings.IndexRune(s, '\n'); idx >= 0 {
s = s[:idx+1]
}
return s
}
// Write writes the help on this topic to the writer.
// If this topic is not known, an error is returned.
func (h Help) Write(w io.Writer) error {
if len(h) == 0 {
return nil
}
if doc, ok := _helpTopics[h]; ok {
_, err := io.WriteString(w, doc)
return errtrace.Wrap(err)
}
topics := make([]string, 0, len(_helpTopics))
for h := range _helpTopics {
topics = append(topics, string(h))
}
sort.Strings(topics)
return errtrace.Wrap(fmt.Errorf("unknown help topic %q: valid values are %q", string(h), topics))
}
var _ flag.Getter = (*Help)(nil)
// Get returns the value of the Help.
// This is to comply with the [flag.Getter] interface.
func (h *Help) Get() any {
return *h
}
// IsBoolFlag marks this as a boolean flag
// which allows it to be used without an argument.
func (*Help) IsBoolFlag() bool {
return true
}
// String returns the name of this topic.
func (h Help) String() string {
return string(h)
}
// Set receives a command line value.
func (h *Help) Set(s string) error {
s = strings.TrimSpace(strings.ToLower(s))
if s == "true" {
s = "default"
}
*h = Help(s)
return nil
}