add pre and post nodes

This commit is contained in:
Daylin Morgan 2024-11-06 21:42:45 -06:00
parent f1c3fdfe78
commit 22fae9ba6a
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -163,7 +163,7 @@ type
hidden*: seq[string] hidden*: seq[string]
subcommands: seq[CliCfg] subcommands: seq[CliCfg]
settings*: set[CliSetting] settings*: set[CliSetting]
run*: NimNode preSub*, postSub*, pre*, post*, run*: NimNode
desc*: NimNode desc*: NimNode
name*: string name*: string
subName*: string # used for help the generator subName*: string # used for help the generator
@ -316,7 +316,6 @@ func isSubMarker(node: NimNode): bool =
func sliceStmts(node: NimNode): seq[ func sliceStmts(node: NimNode): seq[
tuple[name: string, slice: Slice[int]] tuple[name: string, slice: Slice[int]]
] = ] =
if not isSubMarker(node[0]): if not isSubMarker(node[0]):
error "expected a subcommand delimiting line" error "expected a subcommand delimiting line"
@ -403,9 +402,17 @@ func parseCliBody(body: NimNode, name = ""): CliCfg =
result.styles = call[1] result.styles = call[1]
of "required": of "required":
result.required = parseIdentLikeList(call) result.required = parseIdentLikeList(call)
of "preSub":
result.preSub = call[1]
of "postSub":
result.postSub = call[1]
else: else:
error "unknown hwylCli setting: " & name error "unknown hwylCli setting: " & name
for sub in result.subcommands.mitems:
sub.pre = result.preSub
sub.post = result.postSub
result.addGlobalFlagsFrom(result) result.addGlobalFlagsFrom(result)
if result.name == "": if result.name == "":
@ -673,8 +680,12 @@ func hwylCliImpl(cfg: CliCfg, root = false): NimNode =
let runBody = nnkStmtList.newTree() let runBody = nnkStmtList.newTree()
addRequiredFlagsCheck(cfg, runBody) addRequiredFlagsCheck(cfg, runBody)
# move to proc? # move to proc?
if cfg.pre != nil:
runBody.add cfg.pre
if cfg.run != nil: if cfg.run != nil:
runBody.add cfg.run runBody.add cfg.run
if cfg.post != nil:
runBody.add cfg.post
# let runBody = cfg.run or nnkStmtList.newTree(nnkDiscardStmt.newTree(newEmptyNode())) # let runBody = cfg.run or nnkStmtList.newTree(nnkDiscardStmt.newTree(newEmptyNode()))
@ -701,7 +712,7 @@ func hwylCliImpl(cfg: CliCfg, root = false): NimNode =
) )
runBody.add handleSubCommands.add subCommandCase runBody.add handleSubCommands.add subCommandCase
result.add quote do: result.add quote do:
# block: # block:
`printHelperProc` `printHelperProc`
@ -734,6 +745,7 @@ when isMainModule:
config: config:
T seq[string] T seq[string]
? "path to config file" ? "path to config file"
* @["config.yml"]
flags: flags:
check: check:
T bool T bool
@ -749,10 +761,7 @@ when isMainModule:
`long-flag` "some help" `long-flag` "some help"
flagg "some other help" flagg "some other help"
run: run:
echo config
echo "hello from hwylterm sub command!" echo "hello from hwylterm sub command!"
echo `long-flag`
echo flagg
--- b --- b
description "the \"b\" subcommand" description "the \"b\" subcommand"
flags: flags: