mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Markdown supports commands with slashes
This commit is contained in:
parent
8afe9d1b56
commit
500d19e1c2
2 changed files with 30 additions and 6 deletions
|
@ -26,6 +26,14 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func cleanCommandName(name string) string {
|
||||||
|
r := strings.NewReplacer(
|
||||||
|
" ", "_",
|
||||||
|
"/", "_",
|
||||||
|
)
|
||||||
|
return r.Replace(name)
|
||||||
|
}
|
||||||
|
|
||||||
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
|
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
|
||||||
flags := cmd.NonInheritedFlags()
|
flags := cmd.NonInheritedFlags()
|
||||||
flags.SetOutput(buf)
|
flags.SetOutput(buf)
|
||||||
|
@ -82,8 +90,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
|
||||||
if cmd.HasParent() {
|
if cmd.HasParent() {
|
||||||
parent := cmd.Parent()
|
parent := cmd.Parent()
|
||||||
pname := parent.CommandPath()
|
pname := parent.CommandPath()
|
||||||
link := pname + ".md"
|
link := cleanCommandName(pname + ".md")
|
||||||
link = strings.ReplaceAll(link, " ", "_")
|
|
||||||
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))
|
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))
|
||||||
cmd.VisitParents(func(c *cobra.Command) {
|
cmd.VisitParents(func(c *cobra.Command) {
|
||||||
if c.DisableAutoGenTag {
|
if c.DisableAutoGenTag {
|
||||||
|
@ -100,8 +107,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cname := name + " " + child.Name()
|
cname := name + " " + child.Name()
|
||||||
link := cname + ".md"
|
link := cleanCommandName(cname + ".md")
|
||||||
link = strings.ReplaceAll(link, " ", "_")
|
|
||||||
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
|
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
|
||||||
}
|
}
|
||||||
buf.WriteString("\n")
|
buf.WriteString("\n")
|
||||||
|
@ -137,8 +143,9 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".md"
|
basename := cleanCommandName(cmd.CommandPath()) + ".md"
|
||||||
filename := filepath.Join(dir, basename)
|
filename := filepath.Join(dir, basename)
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -95,6 +95,23 @@ func TestGenMdTree(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenMdTreeSlashCommands(t *testing.T) {
|
||||||
|
c := &cobra.Command{Use: "run/first [OPTIONS] arg1 arg2"}
|
||||||
|
tmpdir, err := ioutil.TempDir("", "test-gen-md-tree-slash-commands")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
|
if err := GenMarkdownTree(c, tmpdir); err != nil {
|
||||||
|
t.Fatalf("GenMarkdownTree failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(tmpdir, "run_first.md")); err != nil {
|
||||||
|
t.Fatalf("Expected file 'run_first.md' to exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGenMarkdownToFile(b *testing.B) {
|
func BenchmarkGenMarkdownToFile(b *testing.B) {
|
||||||
file, err := ioutil.TempFile("", "")
|
file, err := ioutil.TempFile("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue