From 0006846c9b846e62e6dd9216c3e418d2bb686998 Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 31 Dec 2025 13:51:01 -0300 Subject: [PATCH 1/2] Fix #59; Allow `&` in command desc --- confutils.nim | 31 ++++++++++++++-- .../snapshots/test_default_cmd_desc_lines.txt | 15 ++++++++ tests/help/test_default_cmd_desc_lines.nim | 35 +++++++++++++++++++ tests/test_help.nim | 3 ++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 tests/help/snapshots/test_default_cmd_desc_lines.txt create mode 100644 tests/help/test_default_cmd_desc_lines.nim 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..5dc1a02 --- /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: +# 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..1c84a7a 100644 --- a/tests/test_help.nim +++ b/tests/test_help.nim @@ -116,3 +116,6 @@ suite "test --help": test "test test_default_cmd_desc printCommand": cmdTest("test_default_cmd_desc", "printCommand") + + test "test test_default_cmd_desc_lines": + cmdTest("test_default_cmd_desc_lines", "") From 953ef1aec0db9b89cab01308c44cddb3d93cb9da Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 31 Dec 2025 15:27:39 -0300 Subject: [PATCH 2/2] wip --- tests/help/test_default_cmd_desc_lines.nim | 2 +- tests/test_help.nim | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/help/test_default_cmd_desc_lines.nim b/tests/help/test_default_cmd_desc_lines.nim index 5dc1a02..2379f86 100644 --- a/tests/help/test_default_cmd_desc_lines.nim +++ b/tests/help/test_default_cmd_desc_lines.nim @@ -19,7 +19,7 @@ type "Multi lines with these " & "triple quoted strings work" -# TODO: +# TODO: https://github.com/nim-lang/Nim/pull/25401 # printCommand = """Multi lines with these # triple quoted strings work""" diff --git a/tests/test_help.nim b/tests/test_help.nim index 1c84a7a..1a1bf7e 100644 --- a/tests/test_help.nim +++ b/tests/test_help.nim @@ -117,5 +117,6 @@ suite "test --help": test "test test_default_cmd_desc printCommand": cmdTest("test_default_cmd_desc", "printCommand") - test "test test_default_cmd_desc_lines": - cmdTest("test_default_cmd_desc_lines", "") + when NimMajor >= 2: + test "test test_default_cmd_desc_lines": + cmdTest("test_default_cmd_desc_lines", "")