@@ -76,37 +76,68 @@ func (w *docWriter) writeCommand(c *Command) {
7676}
7777
7878func (w * docWriter ) writeSubcommand (c * Command ) {
79- fileName := c .FileName
8079 prefix := strings .Repeat ("#" , c .Depth )
81- w .fileMap [fileName ].WriteString (prefix + " " + c .LeafName + "\n \n " )
82- w .fileMap [fileName ].WriteString (c .Description + "\n \n " )
80+ w .fileMap [c . FileName ].WriteString (prefix + " " + c .LeafName + "\n \n " )
81+ w .fileMap [c . FileName ].WriteString (c .Description + "\n \n " )
8382
8483 if isLeafCommand (c ) {
85- w .fileMap [fileName ].WriteString ("Use the following options to change the behavior of this command.\n \n " )
84+ w .fileMap [c . FileName ].WriteString ("Use the following options to change the behavior of this command.\n \n " )
8685
8786 // gather options from command and all options aviailable from parent commands
88- var allOptions = make ([]Option , 0 )
89- for _ , options := range w .optionsStack {
90- allOptions = append (allOptions , options ... )
87+ var options = make ([]Option , 0 )
88+ var globalOptions = make ([]Option , 0 )
89+ for i , o := range w .optionsStack {
90+ if i == len (w .optionsStack )- 1 {
91+ options = append (options , o ... )
92+ } else {
93+ globalOptions = append (globalOptions , o ... )
94+ }
9195 }
9296
9397 // alphabetize options
94- sort .Slice (allOptions , func (i , j int ) bool {
95- return allOptions [i ].Name < allOptions [j ].Name
98+ sort .Slice (options , func (i , j int ) bool {
99+ return options [i ].Name < options [j ].Name
96100 })
97101
98- for _ , option := range allOptions {
99- w .fileMap [c .FileName ].WriteString (fmt .Sprintf ("**--%s**\n \n " , option .Name ))
100- w .fileMap [c .FileName ].WriteString (encodeJSONExample (option .Description ) + "\n \n " )
101- if len (option .Short ) > 0 {
102- w .fileMap [c .FileName ].WriteString ("Alias: `" + option .Short + "`\n \n " )
103- }
102+ sort .Slice (globalOptions , func (i , j int ) bool {
103+ return globalOptions [i ].Name < globalOptions [j ].Name
104+ })
104105
105- if option .Experimental {
106- w .fileMap [fileName ].WriteString (":::note" + "\n \n " )
107- w .fileMap [fileName ].WriteString ("Option is experimental." + "\n \n " )
108- w .fileMap [fileName ].WriteString (":::" + "\n \n " )
109- }
106+ w .writeOptions ("Flags" , options , c )
107+ w .writeOptions ("Global Flags" , globalOptions , c )
108+
109+ }
110+ }
111+
112+ func (w * docWriter ) writeOptions (prefix string , options []Option , c * Command ) {
113+
114+ w .fileMap [c .FileName ].WriteString (fmt .Sprintf ("**%s:**\n \n " , prefix ))
115+
116+ for _ , o := range options {
117+ // option name and alias
118+ w .fileMap [c .FileName ].WriteString (fmt .Sprintf ("**--%s** _%s_" , o .Name , o .Type ))
119+ if len (o .Short ) > 0 {
120+ w .fileMap [c .FileName ].WriteString (fmt .Sprintf (", **-%s** _%s_" , o .Short , o .Type ))
121+ }
122+ w .fileMap [c .FileName ].WriteString ("\n \n " )
123+
124+ // description
125+ w .fileMap [c .FileName ].WriteString (encodeJSONExample (o .Description ))
126+ if o .Required {
127+ w .fileMap [c .FileName ].WriteString (" Required." )
128+ }
129+ if len (o .EnumValues ) > 0 {
130+ w .fileMap [c .FileName ].WriteString (fmt .Sprintf (" Accepted values: %s." , strings .Join (o .EnumValues , ", " )))
131+ }
132+ if len (o .Default ) > 0 {
133+ w .fileMap [c .FileName ].WriteString (fmt .Sprintf (` (default "%s")` , o .Default ))
134+ }
135+ w .fileMap [c .FileName ].WriteString ("\n \n " )
136+
137+ 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 " )
110141 }
111142 }
112143}
0 commit comments