Skip to content

Commit 81c0363

Browse files
authored
DOCSP-26295: Adds annotations for more than one example (#26)
1 parent f7a7f5b commit 81c0363

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

cobra2snooty.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ const syntaxHeader = `Syntax
7272
.. code-block::
7373
`
7474

75-
const examplesHeader = `Examples
76-
--------
77-
78-
.. code-block::
79-
`
80-
8175
const tocHeader = `
8276
.. toctree::
8377
:titlesonly:
@@ -119,9 +113,8 @@ func GenDocs(cmd *cobra.Command, w io.Writer) error {
119113
}
120114
printOptions(buf, cmd)
121115

122-
if len(cmd.Example) > 0 {
123-
buf.WriteString(examplesHeader)
124-
buf.WriteString(fmt.Sprintf("\n%s\n\n", indentString(cmd.Example, " ")))
116+
if cmd.Example != "" {
117+
printExamples(buf, cmd)
125118
}
126119

127120
if hasRelatedCommands(cmd) {

cobra2snooty_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func Echo() *cobra.Command {
7171
Aliases: []string{"say"},
7272
Short: "Echo anything to the screen",
7373
Long: "an utterly useless command for testing",
74-
Example: "Just run root echo",
74+
Example: " # Example with intro text\n atlas command no intro text\n",
7575
Annotations: map[string]string{
7676
"string to printDesc": "A string to print",
7777
"test paramDesc": "just for testing",
@@ -129,7 +129,11 @@ func TestGenDocs(t *testing.T) {
129129
output := buf.String()
130130

131131
checkStringContains(t, output, Echo().Long)
132-
checkStringContains(t, output, Echo().Example)
132+
checkStringContains(t, output, `.. code-block::
133+
134+
# Example with intro text
135+
atlas command no intro text
136+
`)
133137
checkStringContains(t, output, "boolone")
134138
checkStringContains(t, output, "rootflag")
135139
//
@@ -154,7 +158,11 @@ func TestGenDocsNoHiddenParents(t *testing.T) {
154158
output := buf.String()
155159

156160
checkStringContains(t, output, Echo().Long)
157-
checkStringContains(t, output, Echo().Example)
161+
checkStringContains(t, output, `.. code-block::
162+
163+
# Example with intro text
164+
atlas command no intro text
165+
`)
158166
checkStringContains(t, output, "boolone")
159167
checkStringOmits(t, output, "rootflag")
160168
checkStringOmits(t, output, Root().Short)

examples.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cobra2snooty
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
const (
26+
examplesHeader = `Examples
27+
--------
28+
29+
`
30+
identChar = " "
31+
)
32+
33+
func printExamples(buf *bytes.Buffer, cmd *cobra.Command) {
34+
// Create example substrings
35+
examplestrimmed := strings.TrimLeft(cmd.Example, " #")
36+
examples := strings.Split(examplestrimmed, "# ")
37+
buf.WriteString(examplesHeader)
38+
// If it has an example, print the header, then print each in a code block.
39+
for _, example := range examples[0:] {
40+
comment := ""
41+
if strings.Contains(cmd.Example, "#") {
42+
comment = "#"
43+
}
44+
buf.WriteString(`.. code-block::
45+
`)
46+
buf.WriteString(fmt.Sprintf("\n %s%s\n", comment, indentString(example, identChar)))
47+
}
48+
}

0 commit comments

Comments
 (0)