proper hook behavior

This commit is contained in:
Daylin Morgan 2025-01-26 13:31:34 -06:00
parent 2915f65d15
commit 5f2fcdc4ed
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 70 additions and 23 deletions

View file

@ -628,10 +628,10 @@ func postPropagateCheck(c: CliCfg) =
func propagate(c: var CliCfg) = func propagate(c: var CliCfg) =
for child in c.subcommands.mitems: for child in c.subcommands.mitems:
# push the preSub to the lowest subcommand # push the hooks to the lowest subcommand unless another one exists on the way
if child.subcommands.len != 0 and child.preSub == nil: if child.subcommands.len != 0:
child.preSub = c.preSub child.preSub = child.preSub or c.preSub
child.postSub = c.postSub child.postSub = child.postSub or c.postSub
else: else:
child.pre = c.preSub child.pre = c.preSub
child.post = c.postSub child.post = c.postSub
@ -861,8 +861,7 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
result = quote do: result = quote do:
proc `printHelpName`() = proc `printHelpName`() =
echo bb( let help =
render(
newHwylCliHelp( newHwylCliHelp(
header = `header`, header = `header`,
footer = `footer`, footer = `footer`,
@ -872,8 +871,7 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
flags = `helpFlags`, flags = `helpFlags`,
styles = `styles`, styles = `styles`,
) )
) echo help.render().bb()
)
proc checkVal(p: OptParser) = proc checkVal(p: OptParser) =
if p.val == "": if p.val == "":
@ -1314,18 +1312,18 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
let runProcName = ident("run" & name) let runProcName = ident("run" & name)
let runBody = nnkStmtList.newTree() let runBody = nnkStmtList.newTree()
# move to proc?
if cfg.pre != nil: if cfg.pre != nil:
runBody.add cfg.pre runBody.add cfg.pre
if cfg.run != nil:
runBody.add cfg.run
if cfg.post != nil:
runBody.add cfg.post
# args and subcommands need to be mutually exclusive -> implement using a CommandKind? # args and subcommands need to be mutually exclusive -> implement using a CommandKind?
if hasSubcommands cfg: if hasSubcommands cfg:
runBody.add genSubcommandHandler(cfg) runBody.add genSubcommandHandler(cfg)
if cfg.run != nil:
runBody.add cfg.run
if cfg.post != nil:
runBody.add cfg.post
result = newTree(nnkStmtList) result = newTree(nnkStmtList)
result.add quote do: result.add quote do:

View file

@ -0,0 +1,34 @@
import std/[strformat]
import hwylterm, hwylterm/hwylcli
hwylCli:
name "subcommands"
preSub:
echo "preSub from root!"
postSub:
echo "postSub from root!"
subcommands:
[a]
... "subcommand 'a'"
run:
echo "inside sub 'a'"
[b]
... "subcommand 'b'"
run:
echo "inside sub 'b'"
subcommands:
[a]
... "subcommand 'b a'"
run:
echo "inside sub 'b a'"
[c]
... "subcommand 'c'"
preSub:
echo "preSub from 'c'!"
run:
echo "inside sub c"
subcommands:
[a]
... "subcommand 'c a'"
run:
echo "inside sub 'c a'"

View file

@ -1,7 +1,8 @@
import std/[unittest] import std/[os, unittest]
import ./lib import ./lib
preCompileTestModules() if commandLineParams().len == 0:
preCompileTestModules()
suite "hwylcli": suite "hwylcli":
@ -68,3 +69,17 @@ flags:
output (output.txt) output (output.txt)
-h --help -h --help
show this help""") show this help""")
okWithArgs("subHooks", "a", """preSub from root!
inside sub 'a'
postSub from root!""")
okWithArgs("subHooks", "b a", """preSub from root!
inside sub 'b a'
postSub from root!
inside sub 'b'""")
okWithArgs("subHooks", "c a","""preSub from 'c'!
inside sub 'c a'
postSub from root!
inside sub c""")