@@ -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 cmd.opts.len > 0 and cmd.opts[^ 1 ].kind == Arg
@@ -323,9 +325,7 @@ proc describeOptionsList(
323325 appInfo: HelpAppInfo
324326) =
325327 for opt in cmd.opts:
326- if opt.kind == Arg or
327- opt.kind == Discriminator or
328- opt.isHidden:
328+ if not opt.isOpt:
329329 continue
330330
331331 if opt.separator.len > 0 :
@@ -356,27 +356,18 @@ proc describeOptionsList(
356356
357357 helpOutput " \p "
358358
359- # TODO : this is not reached: https://github.com/status-im/nim-confutils/issues/39
360- when false :
361- if opt.kind == Discriminator :
362- for i, subCmd in opt.subCmds:
363- if not subCmd.hasOpts: continue
364-
365- helpOutput " \p When " , styleBright, fgBlue, opt.humaneName, resetStyle, " = " , fgGreen, subCmd.name
366-
367- if i == opt.defaultSubCmd: helpOutput " (default)"
368- help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts
369-
370359proc describeOptions (
371360 help: var string ,
372- cmd: CmdInfo ,
361+ cmds: openArray [ CmdInfo ] ,
373362 cmdInvocation: string ,
374363 appInfo: HelpAppInfo ,
375- optionsType = normalOpts,
376- activeCmds: openArray [CmdInfo ] = @ []
364+ optionsType = normalOpts
377365) =
378- var hasOpts = cmd.hasOpts
379- for c in activeCmds:
366+ if cmds.len == 0 :
367+ return
368+
369+ var hasOpts = false
370+ for c in cmds:
380371 if c.hasOpts:
381372 hasOpts = true
382373
@@ -389,26 +380,37 @@ proc describeOptions(
389380 of defaultCmdOpts:
390381 discard
391382
392- if activeCmds.len > 0 :
393- for c in activeCmds:
394- describeOptionsList (help, c, appInfo)
395- else :
396- describeOptionsList (help, cmd, appInfo)
383+ for c in cmds:
384+ describeOptionsList (help, c, appInfo)
385+
386+ for c in cmds:
387+ for opt in c.opts:
388+ if opt.isOpt and opt.kind == Discriminator :
389+ for i, subCmd in opt.subCmds:
390+ if not subCmd.hasOpts:
391+ continue
392+
393+ helpOutput " \p When " , styleBright, fgBlue, opt.humaneName, resetStyle, " = " , fgGreen, subCmd.name
394+
395+ if i == opt.defaultSubCmd:
396+ helpOutput " (default)"
397+ help.describeOptions [subCmd], cmdInvocation, appInfo, conditionalOpts
397398
399+ let cmd = cmds[^ 1 ]
398400 let subCmdDiscriminator = cmd.getSubCmdDiscriminator
399401 if subCmdDiscriminator != nil :
400402 let defaultCmdIdx = subCmdDiscriminator.defaultSubCmd
401403 if defaultCmdIdx != - 1 :
402404 let defaultCmd = subCmdDiscriminator.subCmds[defaultCmdIdx]
403- help.describeOptions defaultCmd, cmdInvocation, appInfo, defaultCmdOpts
405+ help.describeOptions [ defaultCmd] , cmdInvocation, appInfo, defaultCmdOpts
404406
405407 helpOutput fgSection, " \p Available sub-commands:\p "
406408
407409 for i, subCmd in subCmdDiscriminator.subCmds:
408410 if i != subCmdDiscriminator.defaultSubCmd:
409411 let subCmdInvocation = cmdInvocation & " " & subCmd.name
410412 help.describeInvocation subCmd, subCmdInvocation, appInfo
411- help.describeOptions subCmd, subCmdInvocation, appInfo
413+ help.describeOptions [ subCmd] , subCmdInvocation, appInfo
412414
413415proc showHelp (help: var string ,
414416 appInfo: HelpAppInfo ,
@@ -437,7 +439,7 @@ proc showHelp(help: var string,
437439 # Write out the app or script name
438440 helpOutput fgSection, " Usage: \p "
439441 help.describeInvocation cmd, cmdInvocation, appInfo
440- help.describeOptions cmd , cmdInvocation, appInfo, activeCmds = activeCmds
442+ help.describeOptions activeCmds , cmdInvocation, appInfo
441443 helpOutput " \p "
442444
443445 flushOutputAndQuit QuitSuccess
0 commit comments