add back propagate setting

This commit is contained in:
Daylin Morgan 2025-01-30 17:09:31 -06:00
parent 6a6bd269f2
commit dbde9c91e9
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 69 additions and 10 deletions

View file

@ -206,7 +206,7 @@ proc `$`(c: Count): string = $c.val
type type
CliSetting* = enum CliSetting* = enum
# Propagate, ## Include parent command settings in subcommand Propagate, ## Include parent command settings in subcommand
GenerateOnly, ## Don't attach root `runProc()` node GenerateOnly, ## Don't attach root `runProc()` node
NoHelpFlag, ## Remove the builtin help flag NoHelpFlag, ## Remove the builtin help flag
ShowHelp, ## If cmdline empty show help ShowHelp, ## If cmdline empty show help
@ -460,7 +460,6 @@ func inferShortFlags(cfg: var CliCfg) =
## supplement existing short flags based on initial characters of long flags ## supplement existing short flags based on initial characters of long flags
let taken = cfg.flags.mapIt(it.short).toHashSet() - toHashSet(['\x00']) let taken = cfg.flags.mapIt(it.short).toHashSet() - toHashSet(['\x00'])
var candidates = cfg.flags.mapIt(it.long[0]).toHashSet() - taken var candidates = cfg.flags.mapIt(it.long[0]).toHashSet() - taken
for f in cfg.flags.mitems: for f in cfg.flags.mitems:
if f.short != '\x00' or NoShort in f.settings: continue if f.short != '\x00' or NoShort in f.settings: continue
@ -489,9 +488,6 @@ func postParse(c: var CliCfg) =
if count > 1: if count > 1:
c.err "more than one positional argument is variadic" c.err "more than one positional argument is variadic"
if InferShort in c.settings:
inferShortFlags c
func parseCliFlags(cfg: var CliCfg, node: NimNode) = func parseCliFlags(cfg: var CliCfg, node: NimNode) =
var group: string var group: string
cfg.expectKind node, nnkStmtList cfg.expectKind node, nnkStmtList
@ -579,7 +575,7 @@ func sliceStmts(c: CliCfg, node: NimNode): seq[
# TODO: swap error stmts # TODO: swap error stmts
func inheritFrom(child: var CliCfg, parent: CliCfg) = func inheritFrom(child: var CliCfg, parent: CliCfg) =
## inherit settings from parent command ## inherit flags/groups and settings from parent command
var var
pflags: Table[string, CliFlag] pflags: Table[string, CliFlag]
pgroups: Table[string, seq[CliFlag]] pgroups: Table[string, seq[CliFlag]]
@ -615,6 +611,9 @@ func inheritFrom(child: var CliCfg, parent: CliCfg) =
# so subcommands can continue the inheritance # so subcommands can continue the inheritance
child.flagDefs.add pgroups[g] child.flagDefs.add pgroups[g]
if Propagate in parent.settings:
child.settings = child.settings + parent.settings
func parseCliSubcommands(cfg: var CliCfg, node: NimNode) = func parseCliSubcommands(cfg: var CliCfg, node: NimNode) =
cfg.expectKind node[1], nnkStmtList cfg.expectKind node[1], nnkStmtList
@ -687,7 +686,7 @@ func parseCliAlias(cfg: var CliCfg, node: NimNode) =
cfg.alias.incl s cfg.alias.incl s
else: cfg.unexpectedKind n else: cfg.unexpectedKind n
func postPropagateCheck(c: CliCfg) = func postPropagate(c: var CliCfg) =
## verify the cli is valid ## verify the cli is valid
var var
short: Table[char, CliFlag] short: Table[char, CliFlag]
@ -708,6 +707,9 @@ func postPropagateCheck(c: CliCfg) =
else: else:
long[f.long] = f long[f.long] = f
if InferShort in c.settings:
inferShortFlags c
func propagate(c: var CliCfg) = func propagate(c: var CliCfg) =
for child in c.subcommands.mitems: for child in c.subcommands.mitems:
# push the hooks to the lowest subcommand unless another one exists on the way # push the hooks to the lowest subcommand unless another one exists on the way
@ -717,9 +719,11 @@ func propagate(c: var CliCfg) =
else: else:
child.pre = c.preSub child.pre = c.preSub
child.post = c.postSub child.post = c.postSub
child.inheritFrom(c) child.inheritFrom(c)
propagate child propagate child
postPropagateCheck child postPropagate child
func parseCliHelp(c: var CliCfg, node: NimNode) = func parseCliHelp(c: var CliCfg, node: NimNode) =
@ -891,6 +895,8 @@ func parseCliBody(body: NimNode, name = "", root = false): CliCfg =
if root: if root:
propagate(result) propagate(result)
postPropagate result
func isBool(f: CliFlag | BuiltinFlag): bool = func isBool(f: CliFlag | BuiltinFlag): bool =
f.typeNode == ident"bool" f.typeNode == ident"bool"

View file

@ -0,0 +1,26 @@
import hwylterm, hwylterm/hwylcli
hwylCli:
name "setting-propagate"
settings Propagate, InferShort
flags:
[misc]
input:
T string
* "the default"
? "input flag"
count:
T Count
* Count(val: 0)
? "a count var with default"
subcommands:
[one]
flags:
^[misc]
[two]
settings HideDefault
flags:
^[misc]

View file

@ -66,7 +66,7 @@ flags:
) )
okWithArgs( okWithArgs(
"cliCfgSettingHideDefault", "--help", "hideDefaultSetting", "--help",
""" """
usage: usage:
setting-hide-default [flags] setting-hide-default [flags]
@ -159,7 +159,34 @@ suite "parent-child":
"inheritFlags", "third --misc1", "always=false,misc1=true" "inheritFlags", "third --misc1", "always=false,misc1=true"
) )
suite "settings": suite "settings":
okWithArgs( okWithArgs(
"inferShort", "-i input -o output","""input=input, output=output, count=0, nancy=false, ignore=false""" "inferShort", "-i input -o output","""input=input, output=output, count=0, nancy=false, ignore=false"""
) )
okWithArgs(
"propagateSetting", "one --help",
"""
usage:
setting-propagate one [flags]
flags:
-i --input string input flag (default: the default)
-c --count Count a count var with default (default: 0)
-h --help show this help
"""
)
okWithArgs(
"propagateSetting", "two --help",
"""
usage:
setting-propagate two [flags]
flags:
-i --input string input flag
-c --count Count a count var with default
-h --help show this help
"""
)