Skip to content

Commit c236b99

Browse files
committed
Fix #39; case option description
1 parent 18dc116 commit c236b99

File tree

5 files changed

+122
-27
lines changed

5 files changed

+122
-27
lines changed

confutils.nim

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ proc describeOptionsList(
334334
appInfo: HelpAppInfo
335335
) =
336336
for opt in cmd.opts:
337-
if opt.kind == Arg or
338-
opt.kind == Discriminator or
339-
opt.isHidden:
337+
if not opt.isOpt:
340338
continue
341339

342340
if opt.separator.len > 0:
@@ -367,27 +365,18 @@ proc describeOptionsList(
367365

368366
helpOutput "\p"
369367

370-
# TODO: this is not reached: https://github.com/status-im/nim-confutils/issues/39
371-
when false:
372-
if opt.kind == Discriminator:
373-
for i, subCmd in opt.subCmds:
374-
if not subCmd.hasOpts: continue
375-
376-
helpOutput "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle, " = ", fgGreen, subCmd.name
377-
378-
if i == opt.defaultSubCmd: helpOutput " (default)"
379-
help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts
380-
381368
proc describeOptions(
382369
help: var string,
383-
cmd: CmdInfo,
370+
cmds: openArray[CmdInfo],
384371
cmdInvocation: string,
385372
appInfo: HelpAppInfo,
386-
optionsType = normalOpts,
387-
activeCmds: openArray[CmdInfo] = @[]
373+
optionsType = normalOpts
388374
) =
389-
var hasOpts = cmd.hasOpts
390-
for c in activeCmds:
375+
if cmds.len == 0:
376+
return
377+
378+
var hasOpts = false
379+
for c in cmds:
391380
if c.hasOpts:
392381
hasOpts = true
393382

@@ -400,26 +389,37 @@ proc describeOptions(
400389
of defaultCmdOpts:
401390
discard
402391

403-
if activeCmds.len > 0:
404-
for c in activeCmds:
405-
describeOptionsList(help, c, appInfo)
406-
else:
407-
describeOptionsList(help, cmd, appInfo)
392+
for c in cmds:
393+
describeOptionsList(help, c, appInfo)
394+
395+
for c in cmds:
396+
for opt in c.opts:
397+
if opt.isOpt and opt.kind == Discriminator:
398+
for i, subCmd in opt.subCmds:
399+
if not subCmd.hasOpts:
400+
continue
401+
402+
helpOutput "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle, " = ", fgGreen, subCmd.name
403+
404+
if i == opt.defaultSubCmd:
405+
helpOutput " (default)"
406+
help.describeOptions [subCmd], cmdInvocation, appInfo, conditionalOpts
408407

408+
let cmd = cmds[^1]
409409
let subCmdDiscriminator = cmd.getSubCmdDiscriminator
410410
if subCmdDiscriminator != nil:
411411
let defaultCmdIdx = subCmdDiscriminator.defaultSubCmd
412412
if defaultCmdIdx != -1:
413413
let defaultCmd = subCmdDiscriminator.subCmds[defaultCmdIdx]
414-
help.describeOptions defaultCmd, cmdInvocation, appInfo, defaultCmdOpts
414+
help.describeOptions [defaultCmd], cmdInvocation, appInfo, defaultCmdOpts
415415

416416
helpOutput fgSection, "\pAvailable sub-commands:\p"
417417

418418
for i, subCmd in subCmdDiscriminator.subCmds:
419419
if i != subCmdDiscriminator.defaultSubCmd:
420420
let subCmdInvocation = cmdInvocation & " " & subCmd.name
421421
help.describeInvocation subCmd, subCmdInvocation, appInfo
422-
help.describeOptions subCmd, subCmdInvocation, appInfo
422+
help.describeOptions [subCmd], subCmdInvocation, appInfo
423423

424424
proc showHelp(help: var string,
425425
appInfo: HelpAppInfo,
@@ -448,7 +448,7 @@ proc showHelp(help: var string,
448448
# Write out the app or script name
449449
helpOutput fgSection, "Usage: \p"
450450
help.describeInvocation cmd, cmdInvocation, appInfo
451-
help.describeOptions cmd, cmdInvocation, appInfo, activeCmds = activeCmds
451+
help.describeOptions activeCmds, cmdInvocation, appInfo
452452
helpOutput "\p"
453453

454454
flushOutputAndQuit QuitSuccess
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Usage:
2+
3+
test_case_opt [OPTIONS]... command
4+
5+
The following options are available:
6+
7+
-p, --pre The name of your pre-state (without .ssz) [=pre].
8+
9+
Available sub-commands:
10+
11+
test_case_opt cmdSlotProcessing [OPTIONS]...
12+
13+
The following options are available:
14+
15+
-s, --num-slots The number of slots the pre-state will be advanced by [=1].
16+
17+
test_case_opt cmdBlockProcessing [OPTIONS]...
18+
19+
The following options are available:
20+
21+
--blockProcessingCat block transitions.
22+
23+
When blockProcessingCat = catAttestations, the following additional options are available:
24+
25+
--attestation Attestation filename (without .ssz).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Usage:
2+
3+
test_case_opt cmdBlockProcessing [OPTIONS]...
4+
5+
The following options are available:
6+
7+
-p, --pre The name of your pre-state (without .ssz) [=pre].
8+
--blockProcessingCat block transitions.
9+
10+
When blockProcessingCat = catAttestations, the following additional options are available:
11+
12+
--attestation Attestation filename (without .ssz).

tests/help/test_case_opt.nim

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# confutils
2+
# Copyright (c) 2018-2025 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
import ../../confutils
11+
12+
type
13+
StartupCommand* = enum
14+
noCommand
15+
cmdSlotProcessing
16+
cmdBlockProcessing
17+
18+
BlockProcessingCat* = enum
19+
catBlockHeader
20+
catAttestations
21+
22+
ScenarioConf* = object
23+
preState* {.
24+
desc: "The name of your pre-state (without .ssz)"
25+
name: "pre"
26+
abbr: "p"
27+
defaultValue: "pre".}: string
28+
case cmd*{.
29+
command
30+
defaultValue: noCommand }: StartupCommand
31+
of noCommand:
32+
discard
33+
of cmdSlotProcessing:
34+
numSlots* {.
35+
desc: "The number of slots the pre-state will be advanced by"
36+
name: "num-slots"
37+
abbr: "s"
38+
defaultValue: 1.}: uint64
39+
of cmdBlockProcessing:
40+
case blockProcessingCat* {.
41+
desc: "block transitions"
42+
#name: "process-blocks" # Comment this to make it work
43+
implicitlySelectable
44+
required .}: BlockProcessingCat
45+
of catBlockHeader:
46+
discard
47+
of catAttestations:
48+
attestation*{.
49+
desc: "Attestation filename (without .ssz)"
50+
name: "attestation".}: string
51+
52+
let scenario = ScenarioConf.load(termWidth = int.high)

tests/test_help.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ suite "test --help":
7777

7878
test "test test_longdesc lvl1Cmd1":
7979
cmdTest("test_longdesc", "lvl1Cmd1")
80+
81+
test "test test_case_opt":
82+
cmdTest("test_case_opt", "")
83+
84+
test "test test_case_opt cmdBlockProcessing":
85+
cmdTest("test_case_opt", "cmdBlockProcessing")

0 commit comments

Comments
 (0)