@@ -174,12 +174,14 @@ func isCliSwitch(opt: OptInfo): bool =
174174 opt.kind == CliSwitch or
175175 (opt.kind == Discriminator and opt.isCommand == false )
176176
177+ func isOpt (opt: OptInfo ): bool =
178+ opt.isCliSwitch and not opt.isHidden
179+
177180func hasOpts (cmd: CmdInfo ): bool =
178181 for opt in cmd.opts:
179- if opt.isCliSwitch and not opt.isHidden :
182+ if opt.isOpt :
180183 return true
181-
182- return false
184+ false
183185
184186func hasArgs (cmd: CmdInfo ): bool =
185187 for opt in cmd.opts:
@@ -213,27 +215,43 @@ iterator subCmds(cmd: CmdInfo): CmdInfo =
213215template isSubCommand (cmd: CmdInfo ): bool =
214216 cmd.name.len > 0
215217
216- func maxNameLen (cmd: CmdInfo ): int =
218+ func maxNameLen (cmd: CmdInfo , commands = false ): int =
217219 result = 0
218220 for opt in cmd.opts:
219- if opt.kind == Arg or opt.kind == Discriminator and opt.isCommand:
220- continue
221- result = max (result , opt.name.len)
222- if opt.kind == Discriminator :
221+ if opt.isOpt or opt.kind == Arg :
222+ result = max (result , opt.name.len)
223+ if opt.kind == Discriminator :
224+ for subCmd in opt.subCmds:
225+ result = max (result , maxNameLen (subCmd, commands))
226+ elif commands and opt.kind == Discriminator and opt.isCommand:
223227 for subCmd in opt.subCmds:
224- result = max (result , subCmd.maxNameLen)
228+ result = max (result , maxNameLen (subCmd, commands))
229+
230+ func maxNameLen (cmds: openArray [CmdInfo ]): int =
231+ result = 0
232+ for i, cmd in cmds:
233+ result = max (result , maxNameLen (cmd, commands = i == cmds.high))
225234
226- func hasAbbrs (cmd: CmdInfo ): bool =
235+ func hasAbbrs (cmd: CmdInfo , commands = false ): bool =
227236 for opt in cmd.opts:
228- if opt.kind == Arg or opt.kind == Discriminator and opt.isCommand:
229- continue
230- if opt.abbr.len > 0 :
231- return true
232- if opt.kind == Discriminator :
237+ if opt.isOpt:
238+ if opt.abbr.len > 0 :
239+ return true
240+ if opt.kind == Discriminator :
241+ for subCmd in opt.subCmds:
242+ if hasAbbrs (subCmd, commands):
243+ return true
244+ elif commands and opt.kind == Discriminator and opt.isCommand:
233245 for subCmd in opt.subCmds:
234- if hasAbbrs (subCmd):
246+ if hasAbbrs (subCmd, commands ):
235247 return true
236248
249+ func hasAbbrs (cmds: openArray [CmdInfo ]): bool =
250+ for i, cmd in cmds:
251+ if hasAbbrs (cmd, commands = i == cmds.high):
252+ return true
253+ false
254+
237255func humaneName (opt: OptInfo ): string =
238256 if opt.name.len > 0 : opt.name
239257 else : opt.abbr
@@ -274,7 +292,6 @@ proc describeInvocation(help: var string,
274292 cmd: CmdInfo , cmdInvocation: string ,
275293 appInfo: HelpAppInfo ) =
276294 helpOutput styleBright, " \p " , fgCommand, cmdInvocation
277- var longestArg = 0
278295
279296 if cmd.opts.len > 0 :
280297 if cmd.hasOpts: helpOutput " [OPTIONS]..."
@@ -284,7 +301,6 @@ proc describeInvocation(help: var string,
284301
285302 for arg in cmd.args:
286303 helpOutput " <" , arg.name, " >"
287- longestArg = max (longestArg, arg.name.len)
288304
289305 helpOutput " \p "
290306
@@ -299,9 +315,9 @@ proc describeInvocation(help: var string,
299315 helpOutput " \p "
300316 argsSectionStarted = true
301317
302- let cliArg = " <" & arg.humaneName & " >"
303- helpOutput fgArg, styleBright, cliArg
304- helpOutput padding (cliArg, 6 + appInfo.namesWidth)
318+ let cliArg = " <" & arg.name & " >"
319+ helpOutput fgArg, styleBright, " " , cliArg
320+ helpOutput padding (cliArg, appInfo.namesWidth)
305321 help.writeDesc appInfo, arg.desc, arg.defaultInHelpText
306322 help.writeLongDesc appInfo, arg.longDesc
307323 helpOutput " \p "
@@ -413,8 +429,8 @@ proc showHelp(help: var string,
413429
414430 let cmd = activeCmds[^ 1 ]
415431
416- appInfo.maxNameLen = cmd .maxNameLen
417- appInfo.hasAbbrs = cmd .hasAbbrs
432+ appInfo.maxNameLen = activeCmds .maxNameLen
433+ appInfo.hasAbbrs = activeCmds .hasAbbrs
418434 let termWidth =
419435 try :
420436 terminalWidth ()
0 commit comments