@@ -26,10 +26,7 @@ func genMarkdownDoc(cmd *cobra.Command, dir string) error {
2626 }
2727
2828 basename := strings .ReplaceAll (cmd .CommandPath (), " " , "_" ) + ".md"
29- // clean dupped underscores
30- for strings .Contains (basename , "__" ) {
31- basename = strings .ReplaceAll (basename , "__" , "_" )
32- }
29+ basename = cleanDuppedSeparator (basename , "_" )
3330 filename := filepath .Join (dir , basename )
3431 f , err := os .Create (filename )
3532 if err != nil {
@@ -139,7 +136,7 @@ func printFlags(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
139136// both not deprecated and not the autogenerated help command.
140137// This is a fork of Cobra's `hasSeeAlso` function.
141138func hasSeeAlso (cmd * cobra.Command ) bool {
142- if cmd .HasParent () {
139+ if cmd .HasParent () && cmd . Parent (). IsAvailableCommand () && ! cmd . Parent (). IsAdditionalHelpTopicCommand () {
143140 return true
144141 }
145142 for _ , c := range cmd .Commands () {
@@ -153,11 +150,14 @@ func hasSeeAlso(cmd *cobra.Command) bool {
153150
154151// Print See Also section.
155152func printSeeAlso (buf * bytes.Buffer , cmd * cobra.Command , name string , linkHandler func (s string ) string ) {
156- if cmd .HasParent () {
153+ if cmd .HasParent () && cmd . Parent (). IsAvailableCommand () && ! cmd . Parent (). IsAdditionalHelpTopicCommand () {
157154 parent := cmd .Parent ()
158155 pname := parent .CommandPath ()
156+ pname = cleanDuppedSeparator (pname , " " )
157+ pname = strings .TrimSuffix (pname , " " )
159158 link := pname + ".md"
160159 link = strings .Replace (link , " " , "_" , - 1 )
160+ link = cleanDuppedSeparator (link , "_" )
161161 buf .WriteString (fmt .Sprintf ("\n - [%s](%s) - %s" , pname , linkHandler (link ), parent .Short ))
162162 cmd .VisitParents (func (c * cobra.Command ) {
163163 if c .DisableAutoGenTag {
@@ -174,8 +174,11 @@ func printSeeAlso(buf *bytes.Buffer, cmd *cobra.Command, name string, linkHandle
174174 continue
175175 }
176176 cname := name + " " + child .Name ()
177+ cname = cleanDuppedSeparator (cname , " " )
178+ cname = strings .TrimSuffix (cname , " " )
177179 link := cname + ".md"
178180 link = strings .Replace (link , " " , "_" , - 1 )
181+ link = cleanDuppedSeparator (link , "_" )
179182 buf .WriteString (fmt .Sprintf ("\n - [%s](%s) - %s\n " , cname , linkHandler (link ), child .Short ))
180183 }
181184 buf .WriteString ("\n " )
@@ -186,3 +189,11 @@ type byName []*cobra.Command
186189func (s byName ) Len () int { return len (s ) }
187190func (s byName ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
188191func (s byName ) Less (i , j int ) bool { return s [i ].Name () < s [j ].Name () }
192+
193+ func cleanDuppedSeparator (s string , sep string ) string {
194+ // clean dupped separators
195+ for strings .Contains (s , sep + sep ) {
196+ s = strings .ReplaceAll (s , sep + sep , sep )
197+ }
198+ return s
199+ }
0 commit comments