From 88a0bc2ffed55fbb5e6b6d2f21c129e503beb7e3 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 16 Nov 2024 13:13:05 -0600 Subject: [PATCH] add node to flag definition --- src/hwylterm/hwylcli.nim | 15 ++++++++++----- test.nim | 4 ++++ todo.md | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 test.nim diff --git a/src/hwylterm/hwylcli.nim b/src/hwylterm/hwylcli.nim index 1c748ea..fc79562 100644 --- a/src/hwylterm/hwylcli.nim +++ b/src/hwylterm/hwylcli.nim @@ -159,6 +159,7 @@ type ident*: NimNode default*: NimNode typeNode*: NimNode + node*: NimNode short*: char long*: string help*: NimNode @@ -256,12 +257,14 @@ func parseFlagParams(f: var CliFlag, node: NimNode) = f.ident = getFlagParamNode(n).strVal.ident of "T": f.typeNode = n[1] + of "node": + f.node = n[1] else: error "unexpected setting: " & n[0].strVal else: bad(n, "flag params") -func startFlag(f: var CliFlag, n: NimNode) = +func newFlag(f: var CliFlag, n: NimNode) = f.name = case n[0].kind of nnkIdent, nnkStrLit: n[0].strVal @@ -280,7 +283,7 @@ func parseCliFlag(n: NimNode): CliFlag = if n.kind notin [nnkCommand, nnkCall]: bad(n, "flags") - startFlag(result, n) + newFlag(result, n) # option "some help desc" if n.kind == nnkCommand: result.help = n[1] @@ -761,9 +764,11 @@ func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): N if f.short != '\x00': branch.add(newLit($f.short)) let varName = f.ident let name = newLit(f.name) - branch.add quote do: - flagSet.incl `name` - parse(p, `varName`) + branch.add nnkStmtList.newTree( + nnkCall.newTree(ident("incl"),ident("flagSet"),name), + if f.node == nil: nnkCall.newTree(ident"parse", ident"p", varName) + else: f.node + ) caseStmt.add branch diff --git a/test.nim b/test.nim new file mode 100644 index 0000000..a37f821 --- /dev/null +++ b/test.nim @@ -0,0 +1,4 @@ +import std/macros + +dumpAstGen: + incl(flagSet, name) diff --git a/todo.md b/todo.md index dab022e..8cfa879 100644 --- a/todo.md +++ b/todo.md @@ -25,7 +25,7 @@ - [ ] add flag overlap check before case statement generation (after parsing?) - [ ] add key-value flag support -> `--setting:a:off` - [ ] add defaultFlagType CliCfg setting -- [ ] add node to flagDef to override builtin `parse(p, varName)` +- [x] add node to flagDef to override builtin `parse(p, varName)` ## features