From 8e6aca418259328c7df4a50f8c3bf6d559cc6759 Mon Sep 17 00:00:00 2001 From: Garth Kidd Date: Sun, 17 Jan 2016 11:24:54 +1100 Subject: [PATCH 1/2] Use filepath.Join, not string math. Fixes #226. --- doc/man_docs.go | 5 +++-- doc/md_docs.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/man_docs.go b/doc/man_docs.go index 942b0aa..f21d839 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -18,6 +18,7 @@ import ( "fmt" "io" "os" + "path/filepath" "sort" "strings" "time" @@ -46,8 +47,8 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error { } needToResetTitle := header.Title == "" - filename := cmd.CommandPath() - filename = dir + strings.Replace(filename, " ", "-", -1) + ".1" + basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".1" + filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { return err diff --git a/doc/md_docs.go b/doc/md_docs.go index 666e949..fa13631 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -17,6 +17,7 @@ import ( "fmt" "io" "os" + "path/filepath" "sort" "strings" "time" @@ -156,8 +157,8 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa } } - filename := cmd.CommandPath() - filename = dir + strings.Replace(filename, " ", "_", -1) + ".md" + basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md" + filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { return err From 356750645f4db928552316b8a937aaabb871fdd1 Mon Sep 17 00:00:00 2001 From: Garth Kidd Date: Sun, 17 Jan 2016 11:29:42 +1100 Subject: [PATCH 2/2] Update doc generation examples. Fixes #227. --- doc/man_docs.md | 3 ++- doc/md_docs.md | 35 +++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/doc/man_docs.md b/doc/man_docs.md index 3408c30..5fe957a 100644 --- a/doc/man_docs.md +++ b/doc/man_docs.md @@ -7,6 +7,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" ) func main() { @@ -18,7 +19,7 @@ func main() { Title: "MINE", Section: "3", } - cmd.GenManTree(header, "/tmp") + doc.GenManTree(cmd, header, "/tmp") } ``` diff --git a/doc/md_docs.md b/doc/md_docs.md index da35f92..0c3b96e 100644 --- a/doc/md_docs.md +++ b/doc/md_docs.md @@ -1,5 +1,26 @@ # Generating Markdown Docs For Your Own cobra.Command +Generating man pages from a cobra command is incredibly easy. An example is as follows: + +```go +package main + +import ( + "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" +) + +func main() { + cmd := &cobra.Command{ + Use: "test", + Short: "my test program", + } + doc.GenMarkdownTree(cmd, "/tmp") +} +``` + +That will get you a Markdown document `/tmp/test.md` + ## Generate markdown docs for the entire command tree This program can actually generate docs for the kubectl command in the kubernetes project @@ -11,13 +32,15 @@ import ( "io/ioutil" "os" - "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" - "github.com/spf13/cobra/cobra" + kubectlcmd "k8s.io/kubernetes/pkg/kubectl/cmd" + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" + + "github.com/spf13/cobra/doc" ) func main() { - kubectl := cmd.NewFactory(nil).NewKubectlCommand(os.Stdin, ioutil.Discard, ioutil.Discard) - doc.GenMarkdownTree(kubectl, "./") + cmd := kubectlcmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) + doc.GenMarkdownTree(cmd, "./") } ``` @@ -40,13 +63,13 @@ Both `GenMarkdown` and `GenMarkdownTree` have alternate versions with callbacks ```go func GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error { - //... + //... } ``` ```go func GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error { - //... + //... } ```