prepare to add args CliCfg setting

This commit is contained in:
Daylin Morgan 2024-11-17 12:24:39 -06:00
parent 5a236cd6a5
commit ab00305c92
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -174,6 +174,8 @@ type
CliHelp = object CliHelp = object
header*, footer*, description*, usage*, styles*: NimNode header*, footer*, description*, usage*, styles*: NimNode
CliArg = object
CliCfg = object CliCfg = object
name*: string name*: string
alias*: HashSet[string] # only supported in subcommands alias*: HashSet[string] # only supported in subcommands
@ -187,6 +189,7 @@ type
subcommands: seq[CliCfg] subcommands: seq[CliCfg]
preSub*, postSub*, pre*, post*, run*: NimNode preSub*, postSub*, pre*, post*, run*: NimNode
hidden*: seq[string] hidden*: seq[string]
args*: seq[CliArg]
flags*: seq[CliFlag] flags*: seq[CliFlag]
builtinFlags*: seq[BuiltinFlag] builtinFlags*: seq[BuiltinFlag]
flagDefs*: seq[CliFlag] flagDefs*: seq[CliFlag]
@ -880,6 +883,25 @@ func genSubcommandHandler(cfg: CliCfg): NimNode =
result.add subCommandCase result.add subCommandCase
func parseArgs(p: OptParser, target: var string) =
target = p.key
func parseArgs[T](p: OptParser, target: var seq[T]) =
var val: T
parseArgs(p, val)
target.add val
func argOfBranch(cfg: CliCfg): NimNode =
result = nnkOfBranch.newTree(ident"cmdArgument")
if cfg.args.len == 0 and cfg.subcommands.len == 0:
result.add quote do:
hwylCliError("unexpected positional argument: [b]" & p.key)
else:
result.add quote do:
inc nArgs
parseArgs(p, result)
func hwylCliImpl(cfg: CliCfg): NimNode = func hwylCliImpl(cfg: CliCfg): NimNode =
let let
version = cfg.version or newLit("") version = cfg.version or newLit("")
@ -890,9 +912,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
optParser = ident("p") optParser = ident("p")
cmdLine = ident"cmdLine" cmdLine = ident"cmdLine"
flagSet = ident"flagSet" flagSet = ident"flagSet"
kind = ident"kind" nArgs = ident"nargs"
key = ident"key"
val = ident"val"
(longNoVal, shortNoVal) = cfg.getNoVals() (longNoVal, shortNoVal) = cfg.getNoVals()
printHelpProc = generateCliHelpProc(cfg, printHelpName) printHelpProc = generateCliHelpProc(cfg, printHelpName)
flagVars = setFlagVars(cfg) flagVars = setFlagVars(cfg)
@ -908,9 +928,9 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
stopWords = nnkPrefix.newTree(ident"@", stopWords) stopWords = nnkPrefix.newTree(ident"@", stopWords)
# should this a CritBitTree?
parserBody.add quote do: parserBody.add quote do:
var `flagSet` {.used.}: HashSet[string] var `flagSet`: HashSet[string]
var `nArgs`: int
parserBody.add( parserBody.add(
quote do: quote do:
@ -922,24 +942,23 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
opChars = {','} opChars = {','}
) )
) )
# TODO: first key needs to be normalized
# TODO: first key needs to be normalized?
# TODO: don't use getopt? use p.next() instead?
parserBody.add nnkForStmt.newTree( parserBody.add nnkForStmt.newTree(
kind, key, val, ident"kind", ident"key", ident"val",
nnkCall.newTree(nnkDotExpr.newTree(optParser,ident("getopt"))), # nnkCall.newTree(nnkDotExpr.newTree(optParser,ident("getopt"))),
nnkCall.newTree(ident"getopt", optParser),
nnkStmtList.newTree( nnkStmtList.newTree(
# # for debugging.. # # for debugging..
# quote do: # quote do:
# echo `kind`,"|",`key`,"|",`val` # echo `kind`,"|",`key`,"|",`val`
# , # ,
nnkCaseStmt.newTree( nnkCaseStmt.newTree(
kind, ident"kind",
nnkOfBranch.newTree(ident("cmdError"), quote do: hwylCliError(p.message)), nnkOfBranch.newTree(ident("cmdError"), quote do: hwylCliError(p.message)),
nnkOfBranch.newTree(ident("cmdEnd"), quote do: assert false), nnkOfBranch.newTree(ident("cmdEnd"), quote do: assert false),
# TODO: add nArgs to change how cmdArgument is handled ... argOfBranch(cfg),
nnkOfBranch.newTree(ident("cmdArgument"),
quote do:
result.add `key`
),
nnkOfBranch.newTree( nnkOfBranch.newTree(
ident("cmdShortOption"), ident("cmdLongOption"), ident("cmdShortOption"), ident("cmdLongOption"),
shortLongCaseStmt(cfg, printHelpName, version) shortLongCaseStmt(cfg, printHelpName, version)
@ -986,6 +1005,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
result.add quote do: result.add quote do:
`runProcName`(`args`[1..^1]) `runProcName`(`args`[1..^1])
macro hwylCli*(body: untyped) = macro hwylCli*(body: untyped) =
## generate a CLI styled by `hwylterm` and parsed by `parseopt3` ## generate a CLI styled by `hwylterm` and parsed by `parseopt3`
var cfg = parseCliBody(body, root = true) var cfg = parseCliBody(body, root = true)