|
| 1 | +package docgen |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + "text/template" |
| 10 | + |
| 11 | + "github.com/urfave/cli/v3" |
| 12 | +) |
| 13 | + |
| 14 | +// Markdown renders a Markdown reference for the app. |
| 15 | +// |
| 16 | +// It is adapted from https://sourcegraph.com/github.com/urfave/cli-docs/-/blob/docs.go?L16 |
| 17 | +func Markdown(root *cli.Command) (string, error) { |
| 18 | + var w bytes.Buffer |
| 19 | + if err := writeDocTemplate(root, &w); err != nil { |
| 20 | + return "", err |
| 21 | + } |
| 22 | + return w.String(), nil |
| 23 | +} |
| 24 | + |
| 25 | +type cliTemplate struct { |
| 26 | + App *cli.Command |
| 27 | + Commands []string |
| 28 | + GlobalArgs []string |
| 29 | +} |
| 30 | + |
| 31 | +func writeDocTemplate(root *cli.Command, w io.Writer) error { |
| 32 | + const name = "cli" |
| 33 | + t, err := template.New(name).Parse(markdownDocTemplate) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + return t.ExecuteTemplate(w, name, &cliTemplate{ |
| 38 | + App: root, |
| 39 | + Commands: prepareCommands(root.Name, root.Commands, 0), |
| 40 | + GlobalArgs: prepareArgsWithValues(root.VisibleFlags()), |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func prepareCommands(lineage string, commands []*cli.Command, level int) []string { |
| 45 | + var coms []string |
| 46 | + for _, command := range commands { |
| 47 | + if command.Hidden { |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + var commandDoc strings.Builder |
| 52 | + commandDoc.WriteString(strings.Repeat("#", level+2)) |
| 53 | + commandDoc.WriteString(" ") |
| 54 | + commandDoc.WriteString(fmt.Sprintf("%s %s", lineage, command.Name)) |
| 55 | + commandDoc.WriteString("\n\n") |
| 56 | + commandDoc.WriteString(prepareUsage(command)) |
| 57 | + commandDoc.WriteString("\n\n") |
| 58 | + |
| 59 | + if len(command.Description) > 0 { |
| 60 | + commandDoc.WriteString(fmt.Sprintf("%s\n\n", command.Description)) |
| 61 | + } |
| 62 | + |
| 63 | + commandDoc.WriteString(prepareUsageText(command)) |
| 64 | + |
| 65 | + flags := prepareArgsWithValues(command.Flags) |
| 66 | + if len(flags) > 0 { |
| 67 | + commandDoc.WriteString("\nFlags:\n\n") |
| 68 | + for _, f := range flags { |
| 69 | + commandDoc.WriteString("* " + f) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + coms = append(coms, commandDoc.String()) |
| 74 | + |
| 75 | + // recursevly iterate subcommands |
| 76 | + if len(command.Commands) > 0 { |
| 77 | + coms = append( |
| 78 | + coms, |
| 79 | + prepareCommands(lineage+" "+command.Name, command.Commands, level+1)..., |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return coms |
| 85 | +} |
| 86 | + |
| 87 | +func prepareArgsWithValues(flags []cli.Flag) []string { |
| 88 | + return prepareFlags(flags, ", ", "`", "`", `"<value>"`, true) |
| 89 | +} |
| 90 | + |
| 91 | +func prepareFlags( |
| 92 | + flags []cli.Flag, |
| 93 | + sep, opener, closer, value string, |
| 94 | + addDetails bool, |
| 95 | +) []string { |
| 96 | + args := []string{} |
| 97 | + for _, f := range flags { |
| 98 | + flag, ok := f.(cli.DocGenerationFlag) |
| 99 | + if !ok { |
| 100 | + continue |
| 101 | + } |
| 102 | + modifiedArg := opener |
| 103 | + |
| 104 | + for _, s := range f.Names() { |
| 105 | + trimmed := strings.TrimSpace(s) |
| 106 | + if len(modifiedArg) > len(opener) { |
| 107 | + modifiedArg += sep |
| 108 | + } |
| 109 | + if len(trimmed) > 1 { |
| 110 | + modifiedArg += fmt.Sprintf("--%s", trimmed) |
| 111 | + } else { |
| 112 | + modifiedArg += fmt.Sprintf("-%s", trimmed) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if flag.TakesValue() { |
| 117 | + modifiedArg += fmt.Sprintf("=%s", value) |
| 118 | + } |
| 119 | + |
| 120 | + modifiedArg += closer |
| 121 | + |
| 122 | + if addDetails { |
| 123 | + modifiedArg += flagDetails(flag) |
| 124 | + } |
| 125 | + |
| 126 | + args = append(args, modifiedArg+"\n") |
| 127 | + |
| 128 | + } |
| 129 | + sort.Strings(args) |
| 130 | + return args |
| 131 | +} |
| 132 | + |
| 133 | +// flagDetails returns a string containing the flags metadata |
| 134 | +func flagDetails(flag cli.DocGenerationFlag) string { |
| 135 | + description := flag.GetUsage() |
| 136 | + value := flag.GetValue() |
| 137 | + if value != "" { |
| 138 | + description += " (default: " + value + ")" |
| 139 | + } |
| 140 | + return ": " + description |
| 141 | +} |
| 142 | + |
| 143 | +func prepareUsageText(command *cli.Command) string { |
| 144 | + if command.UsageText == "" { |
| 145 | + if strings.TrimSpace(command.ArgsUsage) != "" { |
| 146 | + return fmt.Sprintf("Arguments: `%s`\n", command.ArgsUsage) |
| 147 | + } |
| 148 | + return "" |
| 149 | + } |
| 150 | + |
| 151 | + // Write all usage examples as a big shell code block |
| 152 | + var usageText strings.Builder |
| 153 | + usageText.WriteString("```sh") |
| 154 | + for line := range strings.SplitSeq(strings.TrimSpace(command.UsageText), "\n") { |
| 155 | + usageText.WriteByte('\n') |
| 156 | + |
| 157 | + line = strings.TrimSpace(line) |
| 158 | + if strings.HasPrefix(line, "# ") { |
| 159 | + usageText.WriteString(line) |
| 160 | + } else if len(line) > 0 { |
| 161 | + usageText.WriteString(fmt.Sprintf("$ %s", line)) |
| 162 | + } |
| 163 | + } |
| 164 | + usageText.WriteString("\n```\n") |
| 165 | + |
| 166 | + return usageText.String() |
| 167 | +} |
| 168 | + |
| 169 | +func prepareUsage(command *cli.Command) string { |
| 170 | + if command.Usage == "" { |
| 171 | + return "" |
| 172 | + } |
| 173 | + |
| 174 | + return command.Usage + "." |
| 175 | +} |
| 176 | + |
| 177 | +var markdownDocTemplate = `# {{ .App.Name }} reference |
| 178 | +
|
| 179 | +{{ .App.Name }}{{ if .App.Usage }} - {{ .App.Usage }}{{ end }} |
| 180 | +{{ if .App.Description }} |
| 181 | +{{ .App.Description }} |
| 182 | +{{ end }} |
| 183 | +` + "```sh" + `{{ if .App.UsageText }} |
| 184 | +{{ .App.UsageText }} |
| 185 | +{{ else }} |
| 186 | +{{ .App.Name }} [GLOBAL FLAGS] command [COMMAND FLAGS] [ARGUMENTS...] |
| 187 | +{{ end }}` + "```" + ` |
| 188 | +{{ if .GlobalArgs }} |
| 189 | +Global flags: |
| 190 | +
|
| 191 | +{{ range $v := .GlobalArgs }}* {{ $v }}{{ end }}{{ end }}{{ if .Commands }} |
| 192 | +{{ range $v := .Commands }} |
| 193 | +{{ $v }}{{ end }}{{ end }}` |
0 commit comments