@@ -18,6 +18,7 @@ func GenerateDocsFiles(commands Commands) (map[string][]byte, error) {
1818 w := & docWriter {
1919 fileMap : make (map [string ]* bytes.Buffer ),
2020 optionSetMap : optionSetMap ,
21+ allCommands : commands .CommandList ,
2122 }
2223
2324 // sorted ascending by full name of command (activity complete, batch list, etc)
@@ -36,6 +37,7 @@ func GenerateDocsFiles(commands Commands) (map[string][]byte, error) {
3637}
3738
3839type docWriter struct {
40+ allCommands []Command
3941 fileMap map [string ]* bytes.Buffer
4042 optionSetMap map [string ]OptionSets
4143 optionsStack [][]Option
@@ -45,16 +47,17 @@ func (c *Command) writeDoc(w *docWriter) error {
4547 w .processOptions (c )
4648
4749 // If this is a root command, write a new file
48- if c .Depth == 1 {
50+ depth := c .depth ()
51+ if depth == 1 {
4952 w .writeCommand (c )
50- } else if c . Depth > 1 {
53+ } else if depth > 1 {
5154 w .writeSubcommand (c )
5255 }
5356 return nil
5457}
5558
5659func (w * docWriter ) writeCommand (c * Command ) {
57- fileName := c .FileName
60+ fileName := c .fileName ()
5861 w .fileMap [fileName ] = & bytes.Buffer {}
5962 w .fileMap [fileName ].WriteString ("---\n " )
6063 w .fileMap [fileName ].WriteString ("id: " + fileName + "\n " )
@@ -76,12 +79,13 @@ func (w *docWriter) writeCommand(c *Command) {
7679}
7780
7881func (w * docWriter ) writeSubcommand (c * Command ) {
79- prefix := strings .Repeat ("#" , c .Depth )
80- w .fileMap [c .FileName ].WriteString (prefix + " " + c .LeafName + "\n \n " )
81- w .fileMap [c .FileName ].WriteString (c .Description + "\n \n " )
82+ fileName := c .fileName ()
83+ prefix := strings .Repeat ("#" , c .depth ())
84+ w .fileMap [fileName ].WriteString (prefix + " " + c .leafName () + "\n \n " )
85+ w .fileMap [fileName ].WriteString (c .Description + "\n \n " )
8286
83- if isLeafCommand (c ) {
84- w .fileMap [c . FileName ].WriteString ("Use the following options to change the behavior of this command.\n \n " )
87+ if w . isLeafCommand (c ) {
88+ w .fileMap [fileName ].WriteString ("Use the following options to change the behavior of this command.\n \n " )
8589
8690 // gather options from command and all options aviailable from parent commands
8791 var options = make ([]Option , 0 )
@@ -110,34 +114,39 @@ func (w *docWriter) writeSubcommand(c *Command) {
110114}
111115
112116func (w * docWriter ) writeOptions (prefix string , options []Option , c * Command ) {
117+ if len (options ) == 0 {
118+ return
119+ }
120+
121+ fileName := c .fileName ()
113122
114- w .fileMap [c . FileName ].WriteString (fmt .Sprintf ("**%s:**\n \n " , prefix ))
123+ w .fileMap [fileName ].WriteString (fmt .Sprintf ("**%s:**\n \n " , prefix ))
115124
116125 for _ , o := range options {
117126 // option name and alias
118- w .fileMap [c . FileName ].WriteString (fmt .Sprintf ("**--%s** _%s_" , o .Name , o .Type ))
127+ w .fileMap [fileName ].WriteString (fmt .Sprintf ("**--%s** _%s_" , o .Name , o .Type ))
119128 if len (o .Short ) > 0 {
120- w .fileMap [c . FileName ].WriteString (fmt .Sprintf (", **-%s** _%s_" , o .Short , o .Type ))
129+ w .fileMap [fileName ].WriteString (fmt .Sprintf (", **-%s** _%s_" , o .Short , o .Type ))
121130 }
122- w .fileMap [c . FileName ].WriteString ("\n \n " )
131+ w .fileMap [fileName ].WriteString ("\n \n " )
123132
124133 // description
125- w .fileMap [c . FileName ].WriteString (encodeJSONExample (o .Description ))
134+ w .fileMap [fileName ].WriteString (encodeJSONExample (o .Description ))
126135 if o .Required {
127- w .fileMap [c . FileName ].WriteString (" Required." )
136+ w .fileMap [fileName ].WriteString (" Required." )
128137 }
129138 if len (o .EnumValues ) > 0 {
130- w .fileMap [c . FileName ].WriteString (fmt .Sprintf (" Accepted values: %s." , strings .Join (o .EnumValues , ", " )))
139+ w .fileMap [fileName ].WriteString (fmt .Sprintf (" Accepted values: %s." , strings .Join (o .EnumValues , ", " )))
131140 }
132141 if len (o .Default ) > 0 {
133- w .fileMap [c . FileName ].WriteString (fmt .Sprintf (` (default "%s")` , o .Default ))
142+ w .fileMap [fileName ].WriteString (fmt .Sprintf (` (default "%s")` , o .Default ))
134143 }
135- w .fileMap [c . FileName ].WriteString ("\n \n " )
144+ w .fileMap [fileName ].WriteString ("\n \n " )
136145
137146 if o .Experimental {
138- w .fileMap [c . FileName ].WriteString (":::note" + "\n \n " )
139- w .fileMap [c . FileName ].WriteString ("Option is experimental." + "\n \n " )
140- w .fileMap [c . FileName ].WriteString (":::" + "\n \n " )
147+ w .fileMap [fileName ].WriteString (":::note" + "\n \n " )
148+ w .fileMap [fileName ].WriteString ("Option is experimental." + "\n \n " )
149+ w .fileMap [fileName ].WriteString (":::" + "\n \n " )
141150 }
142151 }
143152}
@@ -159,6 +168,15 @@ func (w *docWriter) processOptions(c *Command) {
159168 w .optionsStack = append (w .optionsStack , options )
160169}
161170
171+ func (w * docWriter ) isLeafCommand (c * Command ) bool {
172+ for _ , maybeSubCmd := range w .allCommands {
173+ if maybeSubCmd .isSubCommand (c ) {
174+ return false
175+ }
176+ }
177+ return true
178+ }
179+
162180func encodeJSONExample (v string ) string {
163181 // example: 'YourKey={"your": "value"}'
164182 // results in an mdx acorn rendering error
@@ -167,7 +185,3 @@ func encodeJSONExample(v string) string {
167185 v = re .ReplaceAllString (v , "`$1`" )
168186 return v
169187}
170-
171- func isLeafCommand (c * Command ) bool {
172- return len (c .Children ) == 0
173- }
0 commit comments