Skip to content

Commit 6c12fcd

Browse files
committed
Fix #40; Help output regression tests
1 parent fd40a96 commit 6c12fcd

9 files changed

Lines changed: 152 additions & 3 deletions

confutils.nim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ func maxNameLen(cmd: CmdInfo): int =
228228
for subCmd in opt.subCmds:
229229
result = max(result, subCmd.maxNameLen)
230230

231+
func maxNameLen(cmds: openArray[CmdInfo]): int =
232+
result = 0
233+
for cmd in cmds:
234+
result = max(result, cmd.maxNameLen)
235+
231236
func hasAbbrs(cmd: CmdInfo): bool =
232237
for opt in cmd.opts:
233238
if opt.kind == Arg or opt.kind == Discriminator and opt.isCommand:
@@ -239,6 +244,12 @@ func hasAbbrs(cmd: CmdInfo): bool =
239244
if hasAbbrs(subCmd):
240245
return true
241246

247+
func hasAbbrs(cmds: openArray[CmdInfo]): bool =
248+
for cmd in cmds:
249+
if hasAbbrs(cmd):
250+
return true
251+
false
252+
242253
func humaneName(opt: OptInfo): string =
243254
if opt.name.len > 0: opt.name
244255
else: opt.abbr
@@ -418,8 +429,8 @@ proc showHelp(help: var string,
418429

419430
let cmd = activeCmds[^1]
420431

421-
appInfo.maxNameLen = cmd.maxNameLen
422-
appInfo.hasAbbrs = cmd.hasAbbrs
432+
appInfo.maxNameLen = activeCmds.maxNameLen
433+
appInfo.hasAbbrs = activeCmds.hasAbbrs
423434
let termWidth =
424435
try:
425436
terminalWidth()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Usage:
2+
3+
test_default_value_desc [OPTIONS]...
4+
5+
The following options are available:
6+
7+
--opt1 tcp port [=9000].
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Usage:
2+
3+
test_longdesc [OPTIONS]... command
4+
5+
The following options are available:
6+
7+
-o, --opt1 opt1 regular description [=opt1 default].
8+
opt1 longdesc line one.
9+
longdesc line two.
10+
longdesc line three.
11+
12+
Available sub-commands:
13+
14+
test_longdesc lvl1Cmd1 [OPTIONS]...
15+
16+
The following options are available:
17+
18+
--opt2 opt2 regular description [=opt2 default].
19+
opt2 longdesc line one.
20+
longdesc line two.
21+
longdesc line three.
22+
--opt3 opt3 desc [=opt3 default].
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Usage:
2+
3+
test_longdesc lvl1Cmd1 [OPTIONS]...
4+
5+
The following options are available:
6+
7+
-o, --opt1 opt1 regular description [=opt1 default].
8+
opt1 longdesc line one.
9+
longdesc line two.
10+
longdesc line three.
11+
--opt2 opt2 regular description [=opt2 default].
12+
opt2 longdesc line one.
13+
longdesc line two.
14+
longdesc line three.
15+
--opt3 opt3 desc [=opt3 default].
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Usage:
2+
3+
test_separator [OPTIONS]...
4+
5+
The following options are available:
6+
7+
Network Options:
8+
--opt1 opt1 desc [=opt1 default].
9+
--opt2 opt2 desc [=opt2 default].
10+
11+
----------------
12+
--opt3 opt3 desc [=opt3 default].
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import ../../confutils
3+
4+
const defaultEth2TcpPort = 9000
5+
6+
type
7+
TestConf = object
8+
opt1 {.
9+
defaultValue: defaultEth2TcpPort
10+
defaultValueDesc: $defaultEth2TcpPort
11+
desc: "tcp port"
12+
name: "opt1" }: int
13+
14+
let c = TestConf.load(termWidth = int.high)

tests/help/test_longdesc.nim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ../../confutils
2+
3+
type
4+
Lvl1Cmd = enum
5+
lvl1Cmd1
6+
7+
TestConf = object
8+
opt1 {.
9+
desc: "opt1 regular description"
10+
longDesc:
11+
"opt1 longdesc line one\n" &
12+
"longdesc line two\n" &
13+
"longdesc line three"
14+
defaultValue: "opt1 default"
15+
name: "opt1"
16+
abbr: "o" }: string
17+
18+
case cmd {.command.}: Lvl1Cmd
19+
of Lvl1Cmd.lvl1Cmd1:
20+
opt2 {.
21+
desc: "opt2 regular description"
22+
longDesc:
23+
"opt2 longdesc line one\n" &
24+
"longdesc line two\n" &
25+
"longdesc line three"
26+
defaultValue: "opt2 default"
27+
name: "opt2" }: string
28+
opt3 {.
29+
defaultValue: "opt3 default"
30+
desc: "opt3 desc"
31+
name: "opt3" }: string
32+
33+
let c = TestConf.load(termWidth = int.high)

tests/help/test_separator.nim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import ../../confutils
2+
3+
type
4+
Lvl1Cmd = enum
5+
lvl1Cmd1
6+
7+
TestConf = object
8+
opt1 {.
9+
separator: "Network Options:"
10+
defaultValue: "opt1 default"
11+
desc: "opt1 desc"
12+
name: "opt1" }: string
13+
opt2 {.
14+
defaultValue: "opt2 default"
15+
desc: "opt2 desc"
16+
name: "opt2" }: string
17+
opt3 {.
18+
separator: "\p----------------"
19+
defaultValue: "opt3 default"
20+
desc: "opt3 desc"
21+
name: "opt3" }: string
22+
23+
let c = TestConf.load(termWidth = int.high)

tests/test_help.nim

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ 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_default_value_desc":
67+
cmdTest("test_default_value_desc", "")
68+
69+
test "test test_separator":
70+
cmdTest("test_separator", "")
71+
72+
test "test test_longdesc":
73+
cmdTest("test_longdesc", "")
74+
75+
test "test test_longdesc lvl1Cmd1":
76+
cmdTest("test_longdesc", "lvl1Cmd1")

0 commit comments

Comments
 (0)