@@ -144,63 +144,65 @@ internal struct HelpGenerator {
144144 var optionElements : [ Section . Element ] = [ ]
145145 /// Used to keep track of elements already seen from parent commands.
146146 var alreadySeenElements = Set < Section . Element > ( )
147-
148- for commandType in commandStack {
149- let args = Array ( ArgumentSet ( commandType) )
147+
148+ guard let commandType = commandStack. last else {
149+ return [ ]
150+ }
151+
152+ let args = Array ( ArgumentSet ( commandType) )
153+
154+ var i = 0
155+ while i < args. count {
156+ defer { i += 1 }
157+ let arg = args [ i]
150158
151- var i = 0
152- while i < args. count {
153- defer { i += 1 }
154- let arg = args [ i]
155-
156- guard arg. help. help? . shouldDisplay != false else { continue }
157-
158- let synopsis : String
159- let description : String
160-
161- if args [ i] . help. isComposite {
162- // If this argument is composite, we have a group of arguments to
163- // output together.
164- var groupedArgs = [ arg]
165- let defaultValue = arg. help. defaultValue. map { " (default: \( $0) ) " } ?? " "
166- while i < args. count - 1 && args [ i + 1 ] . help. keys == arg. help. keys {
167- groupedArgs. append ( args [ i + 1 ] )
168- i += 1
169- }
159+ guard arg. help. help? . shouldDisplay != false else { continue }
160+
161+ let synopsis : String
162+ let description : String
163+
164+ if args [ i] . help. isComposite {
165+ // If this argument is composite, we have a group of arguments to
166+ // output together.
167+ var groupedArgs = [ arg]
168+ let defaultValue = arg. help. defaultValue. map { " (default: \( $0) ) " } ?? " "
169+ while i < args. count - 1 && args [ i + 1 ] . help. keys == arg. help. keys {
170+ groupedArgs. append ( args [ i + 1 ] )
171+ i += 1
172+ }
170173
171- var synopsisString = " "
172- for arg in groupedArgs {
173- if !synopsisString. isEmpty { synopsisString. append ( " / " ) }
174- synopsisString. append ( " \( arg. synopsisForHelp ?? " " ) " )
175- }
176- synopsis = synopsisString
174+ var synopsisString = " "
175+ for arg in groupedArgs {
176+ if !synopsisString. isEmpty { synopsisString. append ( " / " ) }
177+ synopsisString. append ( " \( arg. synopsisForHelp ?? " " ) " )
178+ }
179+ synopsis = synopsisString
177180
178- var descriptionString : String ?
179- for arg in groupedArgs {
180- if let desc = arg. help. help? . abstract {
181- descriptionString = desc
182- break
183- }
181+ var descriptionString : String ?
182+ for arg in groupedArgs {
183+ if let desc = arg. help. help? . abstract {
184+ descriptionString = desc
185+ break
184186 }
185- description = [ descriptionString, defaultValue]
186- . compactMap { $0 }
187- . joined ( separator: " " )
188- } else {
189- let defaultValue = arg. help. defaultValue. flatMap { $0. isEmpty ? nil : " (default: \( $0) ) " } ?? " "
190- synopsis = arg. synopsisForHelp ?? " "
191- description = [ arg. help. help? . abstract, defaultValue]
192- . compactMap { $0 }
193- . joined ( separator: " " )
194187 }
195-
196- let element = Section . Element ( label: synopsis, abstract: description, discussion: arg. help. help? . discussion ?? " " )
197- if !alreadySeenElements. contains ( element) {
198- alreadySeenElements. insert ( element)
199- if case . positional = arg. kind {
200- positionalElements. append ( element)
201- } else {
202- optionElements. append ( element)
203- }
188+ description = [ descriptionString, defaultValue]
189+ . compactMap { $0 }
190+ . joined ( separator: " " )
191+ } else {
192+ let defaultValue = arg. help. defaultValue. flatMap { $0. isEmpty ? nil : " (default: \( $0) ) " } ?? " "
193+ synopsis = arg. synopsisForHelp ?? " "
194+ description = [ arg. help. help? . abstract, defaultValue]
195+ . compactMap { $0 }
196+ . joined ( separator: " " )
197+ }
198+
199+ let element = Section . Element ( label: synopsis, abstract: description, discussion: arg. help. help? . discussion ?? " " )
200+ if !alreadySeenElements. contains ( element) {
201+ alreadySeenElements. insert ( element)
202+ if case . positional = arg. kind {
203+ positionalElements. append ( element)
204+ } else {
205+ optionElements. append ( element)
204206 }
205207 }
206208 }
0 commit comments