reStructuredText supports commands with slashes

This commit is contained in:
Jim Schubert 2022-04-23 15:38:33 -04:00
parent 385a6fe2ea
commit a913e1fbab
4 changed files with 33 additions and 14 deletions

View file

@ -20,20 +20,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"time" "time"
"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)

View file

@ -70,7 +70,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
if len(long) == 0 { if len(long) == 0 {
long = short long = short
} }
ref := strings.ReplaceAll(name, " ", "_") ref := cleanCommandName(name)
buf.WriteString(".. _" + ref + ":\n\n") buf.WriteString(".. _" + ref + ":\n\n")
buf.WriteString(name + "\n") buf.WriteString(name + "\n")
@ -99,7 +99,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
if cmd.HasParent() { if cmd.HasParent() {
parent := cmd.Parent() parent := cmd.Parent()
pname := parent.CommandPath() pname := parent.CommandPath()
ref = strings.ReplaceAll(pname, " ", "_") ref = cleanCommandName(pname)
buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(pname, ref), parent.Short)) buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(pname, ref), parent.Short))
cmd.VisitParents(func(c *cobra.Command) { cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag { if c.DisableAutoGenTag {
@ -116,7 +116,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
continue continue
} }
cname := name + " " + child.Name() cname := name + " " + child.Name()
ref = strings.ReplaceAll(cname, " ", "_") ref = cleanCommandName(cname)
buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(cname, ref), child.Short)) buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(cname, ref), child.Short))
} }
buf.WriteString("\n") buf.WriteString("\n")
@ -151,7 +151,7 @@ func GenReSTTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string
} }
} }
basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".rst" basename := cleanCommandName(cmd.CommandPath()) + ".rst"
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 {

View file

@ -82,6 +82,24 @@ func TestGenRSTTree(t *testing.T) {
} }
} }
func TestGenRSTTreeSlashCommands(t *testing.T) {
c := &cobra.Command{Use: "run/first [OPTIONS] arg1 arg2"}
tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree-slash-commands")
if err != nil {
t.Fatalf("Failed to create tmpdir: %s", err.Error())
}
defer os.RemoveAll(tmpdir)
if err := GenReSTTree(c, tmpdir); err != nil {
t.Fatalf("GenReSTTree failed: %s", err.Error())
}
if _, err := os.Stat(filepath.Join(tmpdir, "run_first.rst")); err != nil {
t.Fatalf("Expected file 'run_first.rst' to exist")
}
}
func BenchmarkGenReSTToFile(b *testing.B) { func BenchmarkGenReSTToFile(b *testing.B) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {

View file

@ -19,6 +19,16 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// cleanCommandName removes problematic characters from a command's name
// This allows generating commands such as 'perform run/first'.
func cleanCommandName(name string) string {
r := strings.NewReplacer(
" ", "_",
"/", "_",
)
return r.Replace(name)
}
// Test to see if we have a reason to print See Also information in docs // Test to see if we have a reason to print See Also information in docs
// Basically this is a test for a parent command or a subcommand which is // Basically this is a test for a parent command or a subcommand which is
// both not deprecated and not the autogenerated help command. // both not deprecated and not the autogenerated help command.