support default flag type

This commit is contained in:
Daylin Morgan 2024-11-17 12:04:11 -06:00
parent 88a0bc2ffe
commit 5a236cd6a5
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 34 additions and 22 deletions

View file

@ -176,19 +176,20 @@ type
CliCfg = object CliCfg = object
name*: string name*: string
alias*: HashSet[string] alias*: HashSet[string] # only supported in subcommands
version*: NimNode
stopWords*: seq[string] stopWords*: seq[string]
help: CliHelp help: CliHelp
hidden*: seq[string] defaultFlagType: NimNode
subcommands: seq[CliCfg] required*: seq[string]
settings*: set[CliSetting] settings*: set[CliSetting]
subName*: string # used for help generator
subcommands: seq[CliCfg]
preSub*, postSub*, pre*, post*, run*: NimNode preSub*, postSub*, pre*, post*, run*: NimNode
subName*: string # used for help the generator hidden*: seq[string]
version*: NimNode
flags*: seq[CliFlag] flags*: seq[CliFlag]
builtinFlags*: seq[BuiltinFlag] builtinFlags*: seq[BuiltinFlag]
flagDefs*: seq[CliFlag] flagDefs*: seq[CliFlag]
required*: seq[string]
inherit*: Inherit inherit*: Inherit
root*: bool root*: bool
@ -288,6 +289,7 @@ func parseCliFlag(n: NimNode): CliFlag =
if n.kind == nnkCommand: if n.kind == nnkCommand:
result.help = n[1] result.help = n[1]
# option: # option:
# T string
# help "some help description" # help "some help description"
else: else:
parseFlagParams(result, n[1]) parseFlagParams(result, n[1])
@ -297,7 +299,14 @@ func parseCliFlag(n: NimNode): CliFlag =
if result.typeNode == nil: if result.typeNode == nil:
result.typeNode = ident"bool" result.typeNode = ident"bool"
# TODO: change how this works? func postParse(cfg: var CliCfg) =
let defaultTypeNode = cfg.defaultFlagType or ident"bool"
for f in cfg.flagDefs.mitems:
if f.typeNode == nil:
f.typeNode = defaultTypeNode
if f.group in ["", "global"]:
cfg.flags.add f
func parseCliFlags(cfg: var CliCfg, node: NimNode) = func parseCliFlags(cfg: var CliCfg, node: NimNode) =
var group: string var group: string
expectKind node, nnkStmtList expectKind node, nnkStmtList
@ -326,10 +335,8 @@ func parseCliFlags(cfg: var CliCfg, node: NimNode) =
of nnkIdent, nnkStrLit: of nnkIdent, nnkStrLit:
cfg.inherit.flags.add n[1].strval cfg.inherit.flags.add n[1].strval
else: bad(n, "flag") else: bad(n, "flag")
else: bad(n, "flag") else: bad(n, "flag")
cfg.flags = cfg.flagDefs.filterIt(it.group in ["", "global"])
func parseCliSetting(s: string): CliSetting = func parseCliSetting(s: string): CliSetting =
try: parseEnum[CliSetting](s) try: parseEnum[CliSetting](s)
@ -507,8 +514,6 @@ func propagate(c: var CliCfg) =
child.inheritFrom(c) child.inheritFrom(c)
propagate(child) propagate(child)
func parseCliHelp(c: var CliCfg, node: NimNode) = func parseCliHelp(c: var CliCfg, node: NimNode) =
## some possible DSL inputs: ## some possible DSL inputs:
## ##
@ -603,13 +608,16 @@ func parseCliBody(body: NimNode, name = "", root = false): CliCfg =
result.preSub = node[1] result.preSub = node[1]
of "postSub": of "postSub":
result.postSub = node[1] result.postSub = node[1]
of "defaultFlagType":
result.defaultFlagType = node[1]
else: else:
error "unknown hwylCli setting: " & name error "unknown hwylCli setting: " & name
if result.name == "": if result.name == "":
error "missing required option: name" error "missing required option: name"
# TODO: validate "required" flags exist here postParse result
# TODO: validate "required" flags exist here?
result.addBuiltinFlags() result.addBuiltinFlags()
if root: if root:
@ -664,15 +672,19 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
result = quote do: result = quote do:
proc `printHelpName`() = proc `printHelpName`() =
echo bb(render(newHwylCliHelp( echo bb(
header = `header`, render(
footer = `footer`, newHwylCliHelp(
usage = `usage`, header = `header`,
description = `description`, footer = `footer`,
subcmds = `subcmds`, usage = `usage`,
flags = `helpFlags`, description = `description`,
styles = `styles`, subcmds = `subcmds`,
))) flags = `helpFlags`,
styles = `styles`,
)
)
)
proc checkVal(p: OptParser) = proc checkVal(p: OptParser) =
if p.val == "": if p.val == "":

View file

@ -24,7 +24,7 @@
- [x] don't recreate "global"" variables in var section - [x] don't recreate "global"" variables in var section
- [ ] add flag overlap check before case statement generation (after parsing?) - [ ] add flag overlap check before case statement generation (after parsing?)
- [ ] add key-value flag support -> `--setting:a:off` - [ ] add key-value flag support -> `--setting:a:off`
- [ ] add defaultFlagType CliCfg setting - [x] add defaultFlagType CliCfg setting
- [x] add node to flagDef to override builtin `parse(p, varName)` - [x] add node to flagDef to override builtin `parse(p, varName)`
## features ## features