Example #1
0
// Generate prints API docs for a command
func Generate(cmd *cobra.Command) string {
	buf := new(bytes.Buffer)

	cmds := genCmdList(cmd)
	sort.Sort(byName(cmds))
	for _, cmd := range cmds {
		if len(strings.Split(cmd.CommandPath(), " ")) == 1 {
			fmt.Fprint(buf, "**Options**\n\n")
			printOptions(buf, cmd)
			fmt.Fprintln(buf)
			continue
		}

		depth := len(strings.Split(cmd.CommandPath(), " "))

		fmt.Fprint(buf, header(depth, cmd.CommandPath()))

		fmt.Fprint(buf, cmd.Long, "\n\n")

		if cmd.Runnable() {
			fmt.Fprint(buf, "**Usage:** ", "`", cmd.UseLine(), "`", "\n\n")
		}

		if cmd.HasLocalFlags() || cmd.HasPersistentFlags() {
			fmt.Fprint(buf, "**Options**\n\n")
			printOptions(buf, cmd)
		}

		if cmd.Example != "" {
			fmt.Fprint(buf, "**Example**\n\n")
			fmt.Fprint(buf, "```", "\n", cmd.Example, "```", "\n\n")
		}
	}

	return buf.String()
}