forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_reference.go
More file actions
81 lines (64 loc) · 1.55 KB
/
help_reference.go
File metadata and controls
81 lines (64 loc) · 1.55 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
74
75
76
77
78
79
80
81
package root
import (
"fmt"
"strings"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/markdown"
"github.com/spf13/cobra"
)
func referenceHelpFn(cmd *cobra.Command, io *iostreams.IOStreams) func() string {
cs := io.ColorScheme()
return func() string {
reftext := "# gh reference\n\n"
for _, c := range cmd.Commands() {
reftext += cmdRef(cs, c, 0)
}
md, err := markdown.Render(reftext, "auto")
if err != nil {
return reftext
}
return md
}
}
func cmdRef(cs *iostreams.ColorScheme, cmd *cobra.Command, lvl int) string {
ref := ""
if cmd.Hidden {
return ref
}
cmdPrefix := "##"
if lvl > 0 {
cmdPrefix = "###"
}
// Name + Description
escaped := strings.ReplaceAll(cmd.Use, "<", "〈")
escaped = strings.ReplaceAll(escaped, ">", "〉")
ref += fmt.Sprintf("%s %s\n\n", cmdPrefix, escaped)
ref += cmd.Short + "\n\n"
// Flags
// TODO glamour doesn't respect linebreaks (double space or backslash at end) at all, so there is
// no way to have the damn flags print without a whole newline in between.
for _, fu := range strings.Split(cmd.Flags().FlagUsages(), "\n") {
if fu == "" {
continue
}
ref += fu + "\n\n"
}
for _, fu := range strings.Split(cmd.PersistentFlags().FlagUsages(), "\n") {
if fu == "" {
continue
}
ref += fu + "\n\n"
}
if cmd.HasPersistentFlags() || cmd.HasFlags() || cmd.HasAvailableSubCommands() {
ref += "\n"
}
// Subcommands
subcommands := cmd.Commands()
for _, c := range subcommands {
ref += cmdRef(cs, c, lvl+1)
}
if len(subcommands) == 0 {
ref += "\n"
}
return ref
}