Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions confutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ func maxNameLen(cmd: CmdInfo): int =
for subCmd in opt.subCmds:
result = max(result, subCmd.maxNameLen)

func maxNameLen(cmds: openArray[CmdInfo]): int =
result = 0
for cmd in cmds:
result = max(result, cmd.maxNameLen)

func hasAbbrs(cmd: CmdInfo): bool =
for opt in cmd.opts:
if opt.kind == Arg or opt.kind == Discriminator and opt.isCommand:
Expand All @@ -239,6 +244,12 @@ func hasAbbrs(cmd: CmdInfo): bool =
if hasAbbrs(subCmd):
return true

func hasAbbrs(cmds: openArray[CmdInfo]): bool =
for cmd in cmds:
if hasAbbrs(cmd):
return true
false

func humaneName(opt: OptInfo): string =
if opt.name.len > 0: opt.name
else: opt.abbr
Expand Down Expand Up @@ -418,8 +429,8 @@ proc showHelp(help: var string,

let cmd = activeCmds[^1]

appInfo.maxNameLen = cmd.maxNameLen
appInfo.hasAbbrs = cmd.hasAbbrs
appInfo.maxNameLen = activeCmds.maxNameLen
appInfo.hasAbbrs = activeCmds.hasAbbrs
let termWidth =
try:
terminalWidth()
Expand Down
7 changes: 7 additions & 0 deletions tests/help/snapshots/test_default_value_desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Usage:

test_default_value_desc [OPTIONS]...

The following options are available:

--opt1 tcp port [=9000].
22 changes: 22 additions & 0 deletions tests/help/snapshots/test_longdesc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Usage:

test_longdesc [OPTIONS]... command

The following options are available:

-o, --opt1 opt1 regular description [=opt1 default].
opt1 longdesc line one.
longdesc line two.
longdesc line three.

Available sub-commands:

test_longdesc lvl1Cmd1 [OPTIONS]...

The following options are available:

--opt2 opt2 regular description [=opt2 default].
opt2 longdesc line one.
longdesc line two.
longdesc line three.
--opt3 opt3 desc [=opt3 default].
15 changes: 15 additions & 0 deletions tests/help/snapshots/test_longdesc_lvl1Cmd1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Usage:

test_longdesc lvl1Cmd1 [OPTIONS]...

The following options are available:

-o, --opt1 opt1 regular description [=opt1 default].
opt1 longdesc line one.
longdesc line two.
longdesc line three.
--opt2 opt2 regular description [=opt2 default].
opt2 longdesc line one.
longdesc line two.
longdesc line three.
--opt3 opt3 desc [=opt3 default].
12 changes: 12 additions & 0 deletions tests/help/snapshots/test_separator.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Usage:

test_separator [OPTIONS]...

The following options are available:

Network Options:
--opt1 opt1 desc [=opt1 default].
--opt2 opt2 desc [=opt2 default].

----------------
--opt3 opt3 desc [=opt3 default].
22 changes: 22 additions & 0 deletions tests/help/test_default_value_desc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# confutils
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import ../../confutils

const defaultEth2TcpPort = 9000

type
TestConf = object
opt1 {.
defaultValue: defaultEth2TcpPort
defaultValueDesc: $defaultEth2TcpPort
desc: "tcp port"
name: "opt1" }: int

let c = TestConf.load(termWidth = int.high)
42 changes: 42 additions & 0 deletions tests/help/test_longdesc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# confutils
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import ../../confutils

type
Lvl1Cmd = enum
lvl1Cmd1

TestConf = object
opt1 {.
desc: "opt1 regular description"
longDesc:
"opt1 longdesc line one\n" &
"longdesc line two\n" &
"longdesc line three"
defaultValue: "opt1 default"
name: "opt1"
abbr: "o" }: string

case cmd {.command.}: Lvl1Cmd
of Lvl1Cmd.lvl1Cmd1:
opt2 {.
desc: "opt2 regular description"
longDesc:
"opt2 longdesc line one\n" &
"longdesc line two\n" &
"longdesc line three"
defaultValue: "opt2 default"
name: "opt2" }: string
opt3 {.
defaultValue: "opt3 default"
desc: "opt3 desc"
name: "opt3" }: string

let c = TestConf.load(termWidth = int.high)
9 changes: 9 additions & 0 deletions tests/help/test_nested_cmd.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# confutils
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import ../../confutils

type
Expand Down
32 changes: 32 additions & 0 deletions tests/help/test_separator.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# confutils
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import ../../confutils

type
Lvl1Cmd = enum
lvl1Cmd1

TestConf = object
opt1 {.
separator: "Network Options:"
defaultValue: "opt1 default"
desc: "opt1 desc"
name: "opt1" }: string
opt2 {.
defaultValue: "opt2 default"
desc: "opt2 desc"
name: "opt2" }: string
opt3 {.
separator: "\p----------------"
defaultValue: "opt3 default"
desc: "opt3 desc"
name: "opt3" }: string

let c = TestConf.load(termWidth = int.high)
14 changes: 13 additions & 1 deletion tests/test_help.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ suite "test --help":

test "test test_nested_cmd lvl1Cmd1":
cmdTest("test_nested_cmd", "lvl1Cmd1")

test "test test_nested_cmd lvl1Cmd1 lvl2Cmd2":
cmdTest("test_nested_cmd", "lvl1Cmd1 lvl2Cmd2")

test "test test_default_value_desc":
cmdTest("test_default_value_desc", "")

test "test test_separator":
cmdTest("test_separator", "")

test "test test_longdesc":
cmdTest("test_longdesc", "")

test "test test_longdesc lvl1Cmd1":
cmdTest("test_longdesc", "lvl1Cmd1")