@@ -317,51 +317,47 @@ type
317317 defaultCmdOpts
318318 conditionalOpts
319319
320- proc describeOptions (help: var string ,
321- cmd: CmdInfo , cmdInvocation: string ,
322- appInfo: HelpAppInfo , optionsType = normalOpts) =
323- if cmd.hasOpts:
324- case optionsType
325- of normalOpts:
326- helpOutput " \p The following options are available:\p\p "
327- of conditionalOpts:
328- helpOutput " , the following additional options are available:\p\p "
329- of defaultCmdOpts:
330- discard
331-
332- for opt in cmd.opts:
333- if opt.kind == Arg or
334- opt.kind == Discriminator or
335- opt.isHidden: continue
336-
337- if opt.separator.len > 0 :
338- helpOutput opt.separator
339- helpOutput " \p "
320+ proc describeOptionsList (
321+ help: var string ,
322+ cmd: CmdInfo ,
323+ appInfo: HelpAppInfo
324+ ) =
325+ for opt in cmd.opts:
326+ if opt.kind == Arg or
327+ opt.kind == Discriminator or
328+ opt.isHidden:
329+ continue
340330
341- # Indent all command-line switches
342- helpOutput " "
331+ if opt.separator.len > 0 :
332+ helpOutput opt.separator
333+ helpOutput " \p "
343334
344- if opt.abbr.len > 0 :
345- helpOutput fgOption, styleBright, " -" , opt.abbr, " , "
346- elif appInfo.hasAbbrs:
347- # Add additional indentatition, so all names are aligned
348- helpOutput " "
335+ # Indent all command-line switches
336+ helpOutput " "
349337
350- if opt.name.len > 0 :
351- let switch = " --" & opt.name
352- helpOutput fgOption, styleBright,
353- switch, padding (switch, appInfo.namesWidth)
354- else :
355- helpOutput spaces (2 + appInfo.namesWidth)
338+ if opt.abbr.len > 0 :
339+ helpOutput fgOption, styleBright, " -" , opt.abbr, " , "
340+ elif appInfo.hasAbbrs:
341+ # Add additional indentatition, so all names are aligned
342+ helpOutput " "
343+
344+ if opt.name.len > 0 :
345+ let switch = " --" & opt.name
346+ helpOutput fgOption, styleBright,
347+ switch, padding (switch, appInfo.namesWidth)
348+ else :
349+ helpOutput spaces (2 + appInfo.namesWidth)
356350
357- if opt.desc.len > 0 :
358- help.writeDesc appInfo,
359- opt.desc.replace (" %t" , opt.typename),
360- opt.defaultInHelpText
361- help.writeLongDesc appInfo, opt.longDesc
351+ if opt.desc.len > 0 :
352+ help.writeDesc appInfo,
353+ opt.desc.replace (" %t" , opt.typename),
354+ opt.defaultInHelpText
355+ help.writeLongDesc appInfo, opt.longDesc
362356
363- helpOutput " \p "
357+ helpOutput " \p "
364358
359+ # TODO : this is not reached: https://github.com/status-im/nim-confutils/issues/39
360+ when false :
365361 if opt.kind == Discriminator :
366362 for i, subCmd in opt.subCmds:
367363 if not subCmd.hasOpts: continue
@@ -371,6 +367,34 @@ proc describeOptions(help: var string,
371367 if i == opt.defaultSubCmd: helpOutput " (default)"
372368 help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts
373369
370+ proc describeOptions (
371+ help: var string ,
372+ cmd: CmdInfo ,
373+ cmdInvocation: string ,
374+ appInfo: HelpAppInfo ,
375+ optionsType = normalOpts,
376+ activeCmds: openArray [CmdInfo ] = @ []
377+ ) =
378+ var hasOpts = cmd.hasOpts
379+ for c in activeCmds:
380+ if c.hasOpts:
381+ hasOpts = true
382+
383+ if hasOpts:
384+ case optionsType
385+ of normalOpts:
386+ helpOutput " \p The following options are available:\p\p "
387+ of conditionalOpts:
388+ helpOutput " , the following additional options are available:\p\p "
389+ of defaultCmdOpts:
390+ discard
391+
392+ if activeCmds.len > 0 :
393+ for c in activeCmds:
394+ describeOptionsList (help, c, appInfo)
395+ else :
396+ describeOptionsList (help, cmd, appInfo)
397+
374398 let subCmdDiscriminator = cmd.getSubCmdDiscriminator
375399 if subCmdDiscriminator != nil :
376400 let defaultCmdIdx = subCmdDiscriminator.defaultSubCmd
@@ -396,11 +420,13 @@ proc showHelp(help: var string,
396420
397421 appInfo.maxNameLen = cmd.maxNameLen
398422 appInfo.hasAbbrs = cmd.hasAbbrs
399- appInfo.terminalWidth =
423+ let termWidth =
400424 try :
401425 terminalWidth ()
402426 except ValueError :
403427 int .high # https://github.com/nim-lang/Nim/pull/21968
428+ if appInfo.terminalWidth == 0 :
429+ appInfo.terminalWidth = termWidth
404430 appInfo.namesWidth = min (minNameWidth, appInfo.maxNameLen) + descPadding
405431
406432 var cmdInvocation = appInfo.appInvocation
@@ -411,7 +437,7 @@ proc showHelp(help: var string,
411437 # Write out the app or script name
412438 helpOutput fgSection, " Usage: \p "
413439 help.describeInvocation cmd, cmdInvocation, appInfo
414- help.describeOptions cmd, cmdInvocation, appInfo
440+ help.describeOptions cmd, cmdInvocation, appInfo, activeCmds = activeCmds
415441 helpOutput " \p "
416442
417443 flushOutputAndQuit QuitSuccess
@@ -920,7 +946,8 @@ proc loadImpl[C, SecondarySources](
920946 secondarySources: proc (
921947 config: Configuration , sources: ref SecondarySources
922948 ) {.gcsafe , raises : [ConfigurationError ].} = nil ,
923- envVarsPrefix = appInvocation ()
949+ envVarsPrefix = appInvocation (),
950+ termWidth = 0
924951): Configuration {.raises : [ConfigurationError ].} =
925952 # # Loads a program configuration by parsing command-line arguments
926953 # # and a standard set of config files that can specify:
@@ -1094,7 +1121,8 @@ proc loadImpl[C, SecondarySources](
10941121 proc lazyHelpAppInfo : HelpAppInfo =
10951122 HelpAppInfo (
10961123 copyrightBanner: copyrightBanner,
1097- appInvocation: appInvocation ())
1124+ appInvocation: appInvocation (),
1125+ terminalWidth: termWidth)
10981126
10991127 template processHelpAndVersionOptions (optKey: string ) =
11001128 let key = optKey
@@ -1200,12 +1228,13 @@ template load*(
12001228 quitOnFailure = true ,
12011229 ignoreUnknown = false ,
12021230 secondarySources: untyped = nil ,
1203- envVarsPrefix = appInvocation ()): untyped =
1231+ envVarsPrefix = appInvocation (),
1232+ termWidth = 0 ): untyped =
12041233 block :
12051234 let secondarySourcesRef = generateSecondarySources (Configuration )
12061235 loadImpl (Configuration , cmdLine, version,
12071236 copyrightBanner, printUsage, quitOnFailure, ignoreUnknown,
1208- secondarySourcesRef, secondarySources, envVarsPrefix)
1237+ secondarySourcesRef, secondarySources, envVarsPrefix, termWidth )
12091238
12101239func defaults * (Configuration: type ): Configuration =
12111240 load (Configuration , cmdLine = @ [], printUsage = false , quitOnFailure = false )
0 commit comments