Skip to content
Draft
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
31 changes: 29 additions & 2 deletions confutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,36 @@ func findPath(parent, node: CmdInfo): seq[CmdInfo] =

func toText(n: NimNode): string =
if n == nil: ""
elif n.kind in {nnkStrLit..nnkTripleStrLit}: n.strVal
elif n.kind in {nnkStrLit .. nnkTripleStrLit}: n.strVal
else: repr(n)

func concatText(ret: var string, n: NimNode) =
expectKind(n, {nnkInfix, nnkStrLit .. nnkTripleStrLit})
if n.kind == nnkInfix:
let (left, op, right) = unpackInfix(n)
doAssert op == "&", "Invalid string concat"
expectKind(left, {nnkStrLit .. nnkTripleStrLit})
ret.add left.strVal
concatText(ret, right)
else:
ret.add n.strVal

func concatText(n: NimNode): string =
## Concat infixed string: "abc" & "def"
expectKind(n, nnkInfix)
result = ""
concatText(result, n)

func enumText(n: NimNode): string =
expectKind(n, nnkEnumFieldDef)
if n[1].kind in {nnkStrLit .. nnkTripleStrLit}:
n[1].strVal
elif n[1].kind == nnkInfix:
concatText(n[1])
else:
# old behavior; most likely fails
$n[1]

func readPragmaFlags(field: FieldDescription): set[OptFlag] =
result = {}
if field.readPragma("hidden") != nil:
Expand Down Expand Up @@ -946,7 +973,7 @@ proc cmdInfoFromType(T: NimNode): CmdInfo =
var name, desc: string
if enumVal.kind == nnkEnumFieldDef:
name = $enumVal[0]
desc = $enumVal[1]
desc = enumText(enumVal)
else:
name = $enumVal
if defaultValue != nil and eqIdent(name, defaultValue):
Expand Down
15 changes: 15 additions & 0 deletions tests/help/snapshots/test_default_cmd_desc_lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Usage:

test_default_cmd_desc_lines [OPTIONS]... command

This multi line work.

The following options are available:

--help Show this help message and exit.

Available sub-commands:

test_default_cmd_desc_lines printCommand

Multi lines with these triple quoted strings work.
35 changes: 35 additions & 0 deletions tests/help/test_default_cmd_desc_lines.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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
ExporterCmd* = enum
exportCommand =
"This multi " &
"line " &
"work"
printCommand =
"Multi lines with these " &
"triple quoted strings work"

# TODO: https://github.com/nim-lang/Nim/pull/25401
# printCommand = """Multi lines with these
# triple quoted strings work"""

ExporterConf* = object
case cmd* {.
command
defaultValue: exportCommand .}: ExporterCmd
of exportCommand:
discard
of printCommand:
discard

let c = ExporterConf.load(termWidth = int.high)
4 changes: 4 additions & 0 deletions tests/test_help.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,7 @@ suite "test --help":

test "test test_default_cmd_desc printCommand":
cmdTest("test_default_cmd_desc", "printCommand")

when NimMajor >= 2:
test "test test_default_cmd_desc_lines":
cmdTest("test_default_cmd_desc_lines", "")