diff --git a/confutils.nim b/confutils.nim index dc84ea2..d682d51 100644 --- a/confutils.nim +++ b/confutils.nim @@ -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: @@ -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): diff --git a/tests/help/snapshots/test_default_cmd_desc_lines.txt b/tests/help/snapshots/test_default_cmd_desc_lines.txt new file mode 100644 index 0000000..c7a10d4 --- /dev/null +++ b/tests/help/snapshots/test_default_cmd_desc_lines.txt @@ -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. \ No newline at end of file diff --git a/tests/help/test_default_cmd_desc_lines.nim b/tests/help/test_default_cmd_desc_lines.nim new file mode 100644 index 0000000..2379f86 --- /dev/null +++ b/tests/help/test_default_cmd_desc_lines.nim @@ -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) diff --git a/tests/test_help.nim b/tests/test_help.nim index 95549d3..1a1bf7e 100644 --- a/tests/test_help.nim +++ b/tests/test_help.nim @@ -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", "")