Skip to content

Commit 38deb1b

Browse files
committed
Fix #39; case option description
1 parent fd40a96 commit 38deb1b

File tree

5 files changed

+120
-31
lines changed

5 files changed

+120
-31
lines changed

confutils.nim

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
177180
func 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

184186
func 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 "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle, " = ", fgGreen, subCmd.name
366-
367-
if i == opt.defaultSubCmd: helpOutput " (default)"
368-
help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts
369-
370359
proc 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 "\pWhen ", 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, "\pAvailable 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

413415
proc 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
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
import ../../confutils
3+
4+
type
5+
StartupCommand* = enum
6+
noCommand
7+
cmdSlotProcessing
8+
cmdBlockProcessing
9+
10+
BlockProcessingCat* = enum
11+
catBlockHeader
12+
catAttestations
13+
14+
ScenarioConf* = object
15+
preState* {.
16+
desc: "The name of your pre-state (without .ssz)"
17+
name: "pre"
18+
abbr: "p"
19+
defaultValue: "pre".}: string
20+
case cmd*{.
21+
command
22+
defaultValue: noCommand }: StartupCommand
23+
of noCommand:
24+
discard
25+
of cmdSlotProcessing:
26+
numSlots* {.
27+
desc: "The number of slots the pre-state will be advanced by"
28+
name: "num-slots"
29+
abbr: "s"
30+
defaultValue: 1.}: uint64
31+
of cmdBlockProcessing:
32+
case blockProcessingCat* {.
33+
desc: "block transitions"
34+
#name: "process-blocks" # Comment this to make it work
35+
implicitlySelectable
36+
required .}: BlockProcessingCat
37+
of catBlockHeader:
38+
discard
39+
of catAttestations:
40+
attestation*{.
41+
desc: "Attestation filename (without .ssz)"
42+
name: "attestation".}: string
43+
44+
let scenario = ScenarioConf.load()

tests/test_help.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ suite "test --help":
5959

6060
test "test test_nested_cmd lvl1Cmd1":
6161
cmdTest("test_nested_cmd", "lvl1Cmd1")
62-
62+
6363
test "test test_nested_cmd lvl1Cmd1 lvl2Cmd2":
6464
cmdTest("test_nested_cmd", "lvl1Cmd1 lvl2Cmd2")
65+
66+
test "test test_case_opt":
67+
cmdTest("test_case_opt", "")
68+
69+
test "test test_case_opt cmdBlockProcessing":
70+
cmdTest("test_case_opt", "cmdBlockProcessing")

0 commit comments

Comments
 (0)