The github.com/spf13/cobra/doc package provides functions to automatically generate documentation for Cobra commands in multiple formats: man pages, Markdown, reStructuredText, and YAML.
import "github.com/spf13/cobra/doc"type GenManHeader struct {
Title string
Section string
Date *time.Time
Source string
Manual string
}
func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error
func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error
func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) errorGenerate Unix man pages for your command:
package main
import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"time"
)
func main() {
cmd := &cobra.Command{
Use: "myapp",
Short: "My application",
}
// Add subcommands...
header := &doc.GenManHeader{
Title: "MYAPP",
Section: "1",
Date: &time.Time{}, // or nil for current time
Source: "MyApp 1.0",
Manual: "MyApp Manual",
}
// Generate single man page
doc.GenMan(cmd, header, os.Stdout)
// Generate man pages for entire command tree
doc.GenManTree(cmd, header, "/usr/local/share/man/man1")
}Header information for man page generation:
type GenManTreeOptions struct {
Header *GenManHeader
Path string
CommandSeparator string
}Advanced options for man page generation:
opts := doc.GenManTreeOptions{
Header: &doc.GenManHeader{
Title: "MYAPP",
Section: "1",
},
Path: "/usr/local/share/man/man1",
CommandSeparator: "_", // Use underscore instead of dash in filenames
}
doc.GenManTreeFromOpts(cmd, opts)func GenMarkdown(cmd *cobra.Command, w io.Writer) error
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error
func GenMarkdownTree(cmd *cobra.Command, dir string) error
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) errorGenerate Markdown documentation:
package main
import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"os"
)
func main() {
cmd := &cobra.Command{
Use: "myapp",
Short: "My application",
}
// Add subcommands...
// Generate single markdown file
doc.GenMarkdown(cmd, os.Stdout)
// Generate markdown tree
doc.GenMarkdownTree(cmd, "./docs")
// Generate with custom link handler
doc.GenMarkdownCustom(cmd, os.Stdout, func(name string) string {
return "https://example.com/docs/" + name + ".md"
})
// Generate tree with prepender and link handler
doc.GenMarkdownTreeCustom(cmd, "./docs",
func(filename string) string {
return "---\ntitle: Documentation\n---\n\n"
},
func(name string) string {
return "/docs/" + name + ".md"
})
}The linkHandler function transforms command names into documentation links.
The filePrepender function adds content at the beginning of each generated file (e.g., front matter for static site generators).
func GenReST(cmd *cobra.Command, w io.Writer) error
func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, string) string) error
func GenReSTTree(cmd *cobra.Command, dir string) error
func GenReSTTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string, linkHandler func(string, string) string) errorGenerate reStructuredText documentation:
package main
import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"os"
)
func main() {
cmd := &cobra.Command{
Use: "myapp",
Short: "My application",
}
// Add subcommands...
// Generate single ReST file
doc.GenReST(cmd, os.Stdout)
// Generate ReST tree
doc.GenReSTTree(cmd, "./docs")
// Generate with custom link handler
doc.GenReSTCustom(cmd, os.Stdout, func(filename, name string) string {
return ":doc:`" + name + " <" + filename + ">`"
})
// Generate tree with prepender and link handler
doc.GenReSTTreeCustom(cmd, "./docs",
func(filename string) string {
return ".. Documentation for " + filename + "\n\n"
},
func(filename, name string) string {
return ":doc:`" + name + " <" + filename + ">`"
})
}The linkHandler for ReST takes two parameters: filename and name.
func GenYaml(cmd *cobra.Command, w io.Writer) error
func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error
func GenYamlTree(cmd *cobra.Command, dir string) error
func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) errorGenerate YAML documentation:
package main
import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"os"
)
func main() {
cmd := &cobra.Command{
Use: "myapp",
Short: "My application",
}
// Add subcommands...
// Generate single YAML file
doc.GenYaml(cmd, os.Stdout)
// Generate YAML tree
doc.GenYamlTree(cmd, "./docs")
// Generate with custom link handler
doc.GenYamlCustom(cmd, os.Stdout, func(name string) string {
return "docs/" + name + ".yaml"
})
// Generate tree with prepender and link handler
doc.GenYamlTreeCustom(cmd, "./docs",
func(filename string) string {
return "# YAML documentation\n"
},
func(name string) string {
return "docs/" + name + ".yaml"
})
}package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"os"
"time"
)
func main() {
rootCmd := &cobra.Command{
Use: "myapp",
Short: "My application",
Long: "A comprehensive CLI application",
}
serveCmd := &cobra.Command{
Use: "serve",
Short: "Start the server",
}
fetchCmd := &cobra.Command{
Use: "fetch",
Short: "Fetch resources",
}
rootCmd.AddCommand(serveCmd, fetchCmd)
// Generate man pages
manHeader := &doc.GenManHeader{
Title: "MYAPP",
Section: "1",
Date: &time.Time{},
Manual: "MyApp Manual",
}
if err := doc.GenManTree(rootCmd, manHeader, "./man"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Generate Markdown with front matter
if err := doc.GenMarkdownTreeCustom(rootCmd, "./docs/markdown",
func(filename string) string {
return "---\nlayout: doc\n---\n\n"
},
func(name string) string {
return "/docs/" + name + "/"
}); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Generate ReST
if err := doc.GenReSTTree(rootCmd, "./docs/rest"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Generate YAML
if err := doc.GenYamlTree(rootCmd, "./docs/yaml"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Documentation generated successfully!")
}When generating tree documentation, files are named based on the command path:
myapp.mdmyapp_serve.mdmyapp_serve_start.mdCommands with - in names may cause conflicts (e.g., myapp sub-third vs myapp sub third both generate myapp_sub-third.md).
Control whether generated documentation includes the "Auto generated by spf13/cobra" tag:
cmd := &cobra.Command{
Use: "myapp",
DisableAutoGenTag: true, // Omit auto-generated tag
}Integrate documentation generation into your build:
//go:build docgen
// +build docgen
package main
import (
"github.com/spf13/cobra/doc"
"log"
)
func main() {
cmd := getRootCommand() // Your function
if err := doc.GenMarkdownTree(cmd, "./docs"); err != nil {
log.Fatal(err)
}
}Build with:
go run -tags docgen ./cmd/docgenDocumentation includes:
The format varies by output type but covers the same information across all formats.