mirror of
https://github.com/spf13/cobra
synced 2025-04-07 07:19:16 +00:00
Merge 98e8c716ca
into ceb39aba25
This commit is contained in:
commit
32313a8b58
3 changed files with 72 additions and 0 deletions
|
@ -15,6 +15,7 @@
|
||||||
package doc
|
package doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -103,3 +104,9 @@ func checkStringOmits(t *testing.T, got, expected string) {
|
||||||
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
|
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkStringMatch(t *testing.T, got, pattern string) {
|
||||||
|
if ok, _ := regexp.MatchString(pattern, got); !ok {
|
||||||
|
t.Errorf("Expected to match: \n%v\nGot:\n %v\n", pattern, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -156,6 +156,31 @@ func manPreamble(buf io.StringWriter, header *GenManHeader, cmd *cobra.Command,
|
||||||
cobra.WriteStringAndCheck(buf, description+"\n\n")
|
cobra.WriteStringAndCheck(buf, description+"\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func manPrintCommands(buf io.StringWriter, header *GenManHeader, cmd *cobra.Command) {
|
||||||
|
// Find sub-commands that need to be documented
|
||||||
|
subCommands := []*cobra.Command{}
|
||||||
|
for _, c := range cmd.Commands() {
|
||||||
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
subCommands = append(subCommands, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// No need to go further if there is no sub-commands to document
|
||||||
|
if len(subCommands) <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a 'COMMANDS' section in the generated documentation
|
||||||
|
cobra.WriteStringAndCheck(buf, "# COMMANDS\n")
|
||||||
|
// For each sub-commands, and an entry with the command name and it's Short description and reference to dedicated
|
||||||
|
// man page
|
||||||
|
for _, c := range subCommands {
|
||||||
|
dashedPath := strings.Replace(c.CommandPath(), " ", "-", -1)
|
||||||
|
cobra.WriteStringAndCheck(buf, fmt.Sprintf("**%s**\n %s \n See **%s(%s)**.\n\n", c.Name(), c.Short, dashedPath, header.Section))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func manPrintFlags(buf io.StringWriter, flags *pflag.FlagSet) {
|
func manPrintFlags(buf io.StringWriter, flags *pflag.FlagSet) {
|
||||||
flags.VisitAll(func(flag *pflag.Flag) {
|
flags.VisitAll(func(flag *pflag.Flag) {
|
||||||
if len(flag.Deprecated) > 0 || flag.Hidden {
|
if len(flag.Deprecated) > 0 || flag.Hidden {
|
||||||
|
@ -209,6 +234,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
manPreamble(buf, header, cmd, dashCommandName)
|
manPreamble(buf, header, cmd, dashCommandName)
|
||||||
|
manPrintCommands(buf, header, cmd)
|
||||||
manPrintOptions(buf, cmd)
|
manPrintOptions(buf, cmd)
|
||||||
if len(cmd.Example) > 0 {
|
if len(cmd.Example) > 0 {
|
||||||
buf.WriteString("# EXAMPLE\n")
|
buf.WriteString("# EXAMPLE\n")
|
||||||
|
|
|
@ -161,6 +161,45 @@ func TestManPrintFlagsHidesShortDeprecated(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenManCommands(t *testing.T) {
|
||||||
|
header := &GenManHeader{
|
||||||
|
Title: "Project",
|
||||||
|
Section: "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root command
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := GenMan(rootCmd, header, buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
output := buf.String()
|
||||||
|
|
||||||
|
checkStringContains(t, output, ".SH COMMANDS")
|
||||||
|
checkStringMatch(t, output, "\\\\fBecho\\\\fP\n[ \t]+Echo anything to the screen\n[ \t]+See \\\\fBroot-echo\\(2\\)\\\\fP\\\\&\\.")
|
||||||
|
checkStringOmits(t, output, ".PP\n\\fBprint\\fP\n")
|
||||||
|
|
||||||
|
// Echo command
|
||||||
|
buf = new(bytes.Buffer)
|
||||||
|
if err := GenMan(echoCmd, header, buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
output = buf.String()
|
||||||
|
|
||||||
|
checkStringContains(t, output, ".SH COMMANDS")
|
||||||
|
checkStringMatch(t, output, "\\\\fBtimes\\\\fP\n[ \t]+Echo anything to the screen more times\n[ \t]+See \\\\fBroot-echo-times\\(2\\)\\\\fP\\\\&\\.")
|
||||||
|
checkStringMatch(t, output, "\\\\fBechosub\\\\fP\n[ \t]+second sub command for echo\n[ \t]+See \\\\fBroot-echo-echosub\\(2\\)\\\\fP\\\\&\\.")
|
||||||
|
checkStringOmits(t, output, ".PP\n\\fBdeprecated\\fP\n")
|
||||||
|
|
||||||
|
// Time command as echo's subcommand
|
||||||
|
buf = new(bytes.Buffer)
|
||||||
|
if err := GenMan(timesCmd, header, buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
output = buf.String()
|
||||||
|
|
||||||
|
checkStringOmits(t, output, ".SH COMMANDS")
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenManTree(t *testing.T) {
|
func TestGenManTree(t *testing.T) {
|
||||||
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
||||||
header := &GenManHeader{Section: "2"}
|
header := &GenManHeader{Section: "2"}
|
||||||
|
|
Loading…
Reference in a new issue