diff --git a/src/hwylterm/hwylcli.nim b/src/hwylterm/hwylcli.nim index bce8198..89a4575 100644 --- a/src/hwylterm/hwylcli.nim +++ b/src/hwylterm/hwylcli.nim @@ -206,7 +206,7 @@ proc `$`(c: Count): string = $c.val type CliSetting* = enum - # Propagate, ## Include parent command settings in subcommand + Propagate, ## Include parent command settings in subcommand GenerateOnly, ## Don't attach root `runProc()` node NoHelpFlag, ## Remove the builtin help flag 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 let taken = cfg.flags.mapIt(it.short).toHashSet() - toHashSet(['\x00']) var candidates = cfg.flags.mapIt(it.long[0]).toHashSet() - taken - for f in cfg.flags.mitems: if f.short != '\x00' or NoShort in f.settings: continue @@ -488,10 +487,7 @@ func postParse(c: var CliCfg) = let count = c.args.filterIt(it.typeNode.kind == nnkBracketExpr).len if count > 1: c.err "more than one positional argument is variadic" - - if InferShort in c.settings: - inferShortFlags c - + func parseCliFlags(cfg: var CliCfg, node: NimNode) = var group: string cfg.expectKind node, nnkStmtList @@ -579,7 +575,7 @@ func sliceStmts(c: CliCfg, node: NimNode): seq[ # TODO: swap error stmts func inheritFrom(child: var CliCfg, parent: CliCfg) = - ## inherit settings from parent command + ## inherit flags/groups and settings from parent command var pflags: Table[string, CliFlag] pgroups: Table[string, seq[CliFlag]] @@ -615,6 +611,9 @@ func inheritFrom(child: var CliCfg, parent: CliCfg) = # so subcommands can continue the inheritance child.flagDefs.add pgroups[g] + if Propagate in parent.settings: + child.settings = child.settings + parent.settings + func parseCliSubcommands(cfg: var CliCfg, node: NimNode) = cfg.expectKind node[1], nnkStmtList @@ -687,7 +686,7 @@ func parseCliAlias(cfg: var CliCfg, node: NimNode) = cfg.alias.incl s else: cfg.unexpectedKind n -func postPropagateCheck(c: CliCfg) = +func postPropagate(c: var CliCfg) = ## verify the cli is valid var short: Table[char, CliFlag] @@ -708,6 +707,9 @@ func postPropagateCheck(c: CliCfg) = else: long[f.long] = f + if InferShort in c.settings: + inferShortFlags c + func propagate(c: var CliCfg) = for child in c.subcommands.mitems: # push the hooks to the lowest subcommand unless another one exists on the way @@ -717,9 +719,11 @@ func propagate(c: var CliCfg) = else: child.pre = c.preSub child.post = c.postSub + child.inheritFrom(c) + propagate child - postPropagateCheck child + postPropagate child func parseCliHelp(c: var CliCfg, node: NimNode) = @@ -891,6 +895,8 @@ func parseCliBody(body: NimNode, name = "", root = false): CliCfg = if root: propagate(result) + postPropagate result + func isBool(f: CliFlag | BuiltinFlag): bool = f.typeNode == ident"bool" diff --git a/tests/cli/clis/cliCfgSettingHideDefault.nim b/tests/cli/clis/hideDefaultSetting.nim similarity index 100% rename from tests/cli/clis/cliCfgSettingHideDefault.nim rename to tests/cli/clis/hideDefaultSetting.nim diff --git a/tests/cli/clis/propagateSetting.nim b/tests/cli/clis/propagateSetting.nim new file mode 100644 index 0000000..a2a3e44 --- /dev/null +++ b/tests/cli/clis/propagateSetting.nim @@ -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] + + diff --git a/tests/cli/tester.nim b/tests/cli/tester.nim index 78c68cc..25a0640 100644 --- a/tests/cli/tester.nim +++ b/tests/cli/tester.nim @@ -66,7 +66,7 @@ flags: ) okWithArgs( - "cliCfgSettingHideDefault", "--help", + "hideDefaultSetting", "--help", """ usage: setting-hide-default [flags] @@ -159,7 +159,34 @@ suite "parent-child": "inheritFlags", "third --misc1", "always=false,misc1=true" ) + + suite "settings": okWithArgs( "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 +""" + ) +