-
Notifications
You must be signed in to change notification settings - Fork 0
/
help_reference.go
69 lines (59 loc) · 1.44 KB
/
help_reference.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
package root
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/botwayorg/gh/pkg/iostreams"
"github.com/botwayorg/gh/pkg/markdown"
"github.com/spf13/cobra"
)
func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
wrapWidth := 0
style := "notty"
if io.IsStdoutTTY() {
wrapWidth = io.TerminalWidth()
style = markdown.GetStyle(io.DetectTerminalTheme())
}
md, err := markdown.RenderWithWrap(cmd.Long, style, wrapWidth)
if err != nil {
fmt.Fprintln(io.ErrOut, err)
return
}
if !io.IsStdoutTTY() {
fmt.Fprint(io.Out, dedent(md))
return
}
_ = io.StartPager()
defer io.StopPager()
fmt.Fprint(io.Out, md)
}
}
func referenceLong(cmd *cobra.Command) string {
buf := bytes.NewBufferString("# botway reference\n\n")
for _, c := range cmd.Commands() {
if c.Hidden {
continue
}
cmdRef(buf, c, 2)
}
return buf.String()
}
func cmdRef(w io.Writer, cmd *cobra.Command, depth int) {
// Name + Description
fmt.Fprintf(w, "%s `%s`\n\n", strings.Repeat("#", depth), cmd.UseLine())
fmt.Fprintf(w, "%s\n\n", cmd.Short)
// Flags
// TODO: fold in InheritedFlags/PersistentFlags, but omit `--help` due to repetitiveness
if flagUsages := cmd.Flags().FlagUsages(); flagUsages != "" {
fmt.Fprintf(w, "```\n%s````\n\n", dedent(flagUsages))
}
// Subcommands
for _, c := range cmd.Commands() {
if c.Hidden {
continue
}
cmdRef(w, c, depth+1)
}
}