mirror of
https://github.com/spf13/cobra
synced 2024-11-16 10:47:09 +00:00
adds a flag to disable autogen tag at the bottom of man and markdown generators. Inherits from parent commands all the way to root
This commit is contained in:
parent
62e859a9ed
commit
cbd1914f28
6 changed files with 105 additions and 10 deletions
|
@ -651,6 +651,34 @@ func TestRunnableRootCommand(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestVisitParents(t *testing.T) {
|
||||
c := &Command{Use: "app"}
|
||||
sub := &Command{Use: "sub"}
|
||||
dsub := &Command{Use: "dsub"}
|
||||
sub.AddCommand(dsub)
|
||||
c.AddCommand(sub)
|
||||
total := 0
|
||||
add := func(x *Command) {
|
||||
total++
|
||||
}
|
||||
sub.VisitParents(add)
|
||||
if total != 1 {
|
||||
t.Errorf("Should have visited 1 parent but visited %d", total)
|
||||
}
|
||||
|
||||
total = 0
|
||||
dsub.VisitParents(add)
|
||||
if total != 2 {
|
||||
t.Errorf("Should have visited 2 parent but visited %d", total)
|
||||
}
|
||||
|
||||
total = 0
|
||||
c.VisitParents(add)
|
||||
if total != 0 {
|
||||
t.Errorf("Should have not visited any parent but visited %d", total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnableRootCommandNilInput(t *testing.T) {
|
||||
empty_arg := make([]string, 0)
|
||||
c := initializeWithRootCmd()
|
||||
|
|
20
command.go
20
command.go
|
@ -92,6 +92,8 @@ type Command struct {
|
|||
PersistentPostRun func(cmd *Command, args []string)
|
||||
// PersistentPostRunE: PersistentPostRun but returns an error
|
||||
PersistentPostRunE func(cmd *Command, args []string) error
|
||||
// DisableAutoGenTag remove
|
||||
DisableAutoGenTag bool
|
||||
// Commands is the list of commands supported by this program.
|
||||
commands []*Command
|
||||
// Parent Command for this command
|
||||
|
@ -473,15 +475,29 @@ func (c *Command) SuggestionsFor(typedName string) []string {
|
|||
return suggestions
|
||||
}
|
||||
|
||||
func (c *Command) VisitParents(fn func(*Command)) {
|
||||
var traverse func(*Command) *Command
|
||||
|
||||
traverse = func(x *Command) *Command {
|
||||
if x != c {
|
||||
fn(x)
|
||||
}
|
||||
if x.HasParent() {
|
||||
return traverse(x.parent)
|
||||
}
|
||||
return x
|
||||
}
|
||||
traverse(c)
|
||||
}
|
||||
|
||||
func (c *Command) Root() *Command {
|
||||
var findRoot func(*Command) *Command
|
||||
|
||||
findRoot = func(x *Command) *Command {
|
||||
if x.HasParent() {
|
||||
return findRoot(x.parent)
|
||||
} else {
|
||||
return x
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
return findRoot(c)
|
||||
|
|
13
man_docs.go
13
man_docs.go
|
@ -183,20 +183,22 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte {
|
|||
|
||||
manPreamble(buf, header, commandName, short, long)
|
||||
manPrintOptions(buf, cmd)
|
||||
|
||||
if len(cmd.Example) > 0 {
|
||||
fmt.Fprintf(buf, "# EXAMPLE\n")
|
||||
fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)
|
||||
}
|
||||
|
||||
if cmd.hasSeeAlso() {
|
||||
fmt.Fprintf(buf, "# SEE ALSO\n")
|
||||
if cmd.HasParent() {
|
||||
parentPath := cmd.Parent().CommandPath()
|
||||
dashParentPath := strings.Replace(parentPath, " ", "-", -1)
|
||||
fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section)
|
||||
cmd.VisitParents(func(c *Command) {
|
||||
if c.DisableAutoGenTag {
|
||||
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
children := cmd.Commands()
|
||||
sort.Sort(byName(children))
|
||||
for _, c := range children {
|
||||
|
@ -207,7 +209,8 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte {
|
|||
}
|
||||
fmt.Fprintf(buf, "\n")
|
||||
}
|
||||
|
||||
fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
|
||||
if !cmd.DisableAutoGenTag {
|
||||
fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
|
|
@ -65,4 +65,30 @@ func TestGenManDoc(t *testing.T) {
|
|||
|
||||
unexpected := translate(cmdDeprecated.Name())
|
||||
checkStringOmits(t, found, unexpected)
|
||||
|
||||
// auto generated
|
||||
expected = translate("Auto generated")
|
||||
checkStringContains(t, found, expected)
|
||||
}
|
||||
|
||||
func TestGenManNoGenTag(t *testing.T) {
|
||||
|
||||
c := initializeWithRootCmd()
|
||||
// Need two commands to run the command alphabetical sort
|
||||
cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
||||
cmdEcho.DisableAutoGenTag = true
|
||||
out := new(bytes.Buffer)
|
||||
|
||||
header := &GenManHeader{
|
||||
Title: "Project",
|
||||
Section: "2",
|
||||
}
|
||||
// We generate on a subcommand so we have both subcommands and parents
|
||||
cmdEcho.GenMan(header, out)
|
||||
found := out.String()
|
||||
|
||||
unexpected := translate("#HISTORY")
|
||||
checkStringOmits(t, found, unexpected)
|
||||
}
|
||||
|
|
11
md_docs.go
11
md_docs.go
|
@ -82,7 +82,6 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
|
|||
}
|
||||
|
||||
printOptions(out, cmd, name)
|
||||
|
||||
if cmd.hasSeeAlso() {
|
||||
fmt.Fprintf(out, "### SEE ALSO\n")
|
||||
if cmd.HasParent() {
|
||||
|
@ -91,6 +90,11 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
|
|||
link := pname + ".md"
|
||||
link = strings.Replace(link, " ", "_", -1)
|
||||
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
|
||||
cmd.VisitParents(func(c *Command) {
|
||||
if c.DisableAutoGenTag {
|
||||
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
children := cmd.Commands()
|
||||
|
@ -107,8 +111,9 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
|
|||
}
|
||||
fmt.Fprintf(out, "\n")
|
||||
}
|
||||
|
||||
fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))
|
||||
if !cmd.DisableAutoGenTag {
|
||||
fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))
|
||||
}
|
||||
}
|
||||
|
||||
func GenMarkdownTree(cmd *Command, dir string) {
|
||||
|
|
|
@ -65,3 +65,20 @@ func TestGenMdDoc(t *testing.T) {
|
|||
t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenMdNoTag(t *testing.T) {
|
||||
c := initializeWithRootCmd()
|
||||
// Need two commands to run the command alphabetical sort
|
||||
cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.DisableAutoGenTag = true
|
||||
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
||||
out := new(bytes.Buffer)
|
||||
|
||||
GenMarkdown(c, out)
|
||||
found := out.String()
|
||||
|
||||
unexpected := "Auto generated"
|
||||
checkStringOmits(t, found, unexpected)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue