mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 19:10:45 -06:00
Compare commits
No commits in common. "6082cc3835ca9ef25570dcea3a43f565627145cd" and "cbeefd675c0884feebad4dc62910092519f8b2ed" have entirely different histories.
6082cc3835
...
cbeefd675c
3 changed files with 48 additions and 110 deletions
|
@ -25,7 +25,8 @@ type
|
||||||
cmd* = "bold"
|
cmd* = "bold"
|
||||||
settings*: set[HwylCliStyleSetting] = {Aliases}
|
settings*: set[HwylCliStyleSetting] = {Aliases}
|
||||||
HwylCliHelp* = object
|
HwylCliHelp* = object
|
||||||
header*, footer*, description*, usage*: string
|
usage*: string
|
||||||
|
desc*: string
|
||||||
subcmds*: seq[HwylSubCmdHelp]
|
subcmds*: seq[HwylSubCmdHelp]
|
||||||
flags*: seq[HwylFlagHelp]
|
flags*: seq[HwylFlagHelp]
|
||||||
styles*: HwylCliStyles
|
styles*: HwylCliStyles
|
||||||
|
@ -36,15 +37,13 @@ func firstLine(s: string): string =
|
||||||
s.strip().dedent().strip().splitlines()[0]
|
s.strip().dedent().strip().splitlines()[0]
|
||||||
|
|
||||||
func newHwylCliHelp*(
|
func newHwylCliHelp*(
|
||||||
header = "",
|
|
||||||
usage = "",
|
usage = "",
|
||||||
footer = "",
|
desc = "",
|
||||||
description = "",
|
|
||||||
subcmds: openArray[HwylSubCmdHelp] = @[],
|
subcmds: openArray[HwylSubCmdHelp] = @[],
|
||||||
flags: openArray[HwylFlagHelp] = @[],
|
flags: openArray[HwylFlagHelp] = @[],
|
||||||
styles = HwylCliStyles()
|
styles = HwylCliStyles()
|
||||||
): HwylCliHelp =
|
): HwylCliHelp =
|
||||||
result.description = dedent(description).strip()
|
result.desc = dedent(desc).strip()
|
||||||
if Aliases in styles.settings:
|
if Aliases in styles.settings:
|
||||||
result.subcmds =
|
result.subcmds =
|
||||||
subcmds.mapIt((it.name & " " & it.aliases, it.aliases, it.desc.firstLine))
|
subcmds.mapIt((it.name & " " & it.aliases, it.aliases, it.desc.firstLine))
|
||||||
|
@ -100,17 +99,14 @@ func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
||||||
|
|
||||||
# TODO: split this into separate procs to make overriding more fluid
|
# TODO: split this into separate procs to make overriding more fluid
|
||||||
func render*(cli: HwylCliHelp): string =
|
func render*(cli: HwylCliHelp): string =
|
||||||
if cli.header != "":
|
|
||||||
result.add cli.header
|
|
||||||
result.add "\n"
|
|
||||||
if cli.usage != "":
|
if cli.usage != "":
|
||||||
result.add "[" & cli.styles.header & "]"
|
result.add "[" & cli.styles.header & "]"
|
||||||
result.add "usage[/]:\n"
|
result.add "usage[/]:\n"
|
||||||
result.add indent(cli.usage, 2 )
|
result.add indent(cli.usage, 2 )
|
||||||
result.add "\n"
|
result.add "\n"
|
||||||
if cli.description != "":
|
if cli.desc != "":
|
||||||
result.add "\n"
|
result.add "\n"
|
||||||
result.add cli.description
|
result.add cli.desc
|
||||||
result.add "\n"
|
result.add "\n"
|
||||||
if cli.subcmds.len > 0:
|
if cli.subcmds.len > 0:
|
||||||
result.add "\n"
|
result.add "\n"
|
||||||
|
@ -124,8 +120,6 @@ func render*(cli: HwylCliHelp): string =
|
||||||
result.add "flags[/]:\n"
|
result.add "flags[/]:\n"
|
||||||
for f in cli.flags:
|
for f in cli.flags:
|
||||||
result.add render(cli,f)
|
result.add render(cli,f)
|
||||||
if cli.footer != "":
|
|
||||||
result.add cli.footer
|
|
||||||
|
|
||||||
proc bb*(cli: HwylCliHelp): BbString =
|
proc bb*(cli: HwylCliHelp): BbString =
|
||||||
result = bb(render(cli))
|
result = bb(render(cli))
|
||||||
|
@ -167,20 +161,18 @@ type
|
||||||
flags: seq[string]
|
flags: seq[string]
|
||||||
groups: seq[string]
|
groups: seq[string]
|
||||||
|
|
||||||
CliHelp = object
|
|
||||||
header*, footer*, description*, usage*, styles*: NimNode
|
|
||||||
|
|
||||||
CliCfg = object
|
CliCfg = object
|
||||||
name*: string
|
name*: string
|
||||||
alias*: HashSet[string]
|
alias*: HashSet[string]
|
||||||
stopWords*: seq[string]
|
stopWords*: seq[string]
|
||||||
help: CliHelp
|
styles: NimNode
|
||||||
hidden*: seq[string]
|
hidden*: seq[string]
|
||||||
subcommands: seq[CliCfg]
|
subcommands: seq[CliCfg]
|
||||||
settings*: set[CliSetting]
|
settings*: set[CliSetting]
|
||||||
preSub*, postSub*, pre*, post*, run*: NimNode
|
preSub*, postSub*, pre*, post*, run*: NimNode
|
||||||
|
desc*: NimNode
|
||||||
subName*: string # used for help the generator
|
subName*: string # used for help the generator
|
||||||
version*: NimNode
|
version*, usage*: NimNode
|
||||||
flags*: seq[CliFlag]
|
flags*: seq[CliFlag]
|
||||||
builtinFlags*: seq[BuiltinFlag]
|
builtinFlags*: seq[BuiltinFlag]
|
||||||
flagDefs*: seq[CliFlag]
|
flagDefs*: seq[CliFlag]
|
||||||
|
@ -502,110 +494,60 @@ func propagate(c: var CliCfg) =
|
||||||
propagate(child)
|
propagate(child)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func parseCliHelp(c: var CliCfg, node: NimNode) =
|
|
||||||
## some possible DSL inputs:
|
|
||||||
##
|
|
||||||
## ```
|
|
||||||
## help:
|
|
||||||
## header NimNode -> string
|
|
||||||
## usage NimNode -> string
|
|
||||||
## description NimNode -> string
|
|
||||||
## footer NimNode -> string
|
|
||||||
## styles NimNode -> HwylCliStyles()
|
|
||||||
## ```
|
|
||||||
##
|
|
||||||
## ```
|
|
||||||
## help NimNode
|
|
||||||
## ```
|
|
||||||
##
|
|
||||||
## ```
|
|
||||||
## ... NimNode
|
|
||||||
## `
|
|
||||||
|
|
||||||
expectLen node, 2
|
|
||||||
var help: CliHelp = c.help
|
|
||||||
case node.kind:
|
|
||||||
# help NimNode or ... NimNode
|
|
||||||
of nnkPrefix, nnkCommand:
|
|
||||||
help.description = node[1]
|
|
||||||
# help:
|
|
||||||
# description NimNode
|
|
||||||
# usage: NimNode
|
|
||||||
of nnkCall:
|
|
||||||
if node[1].kind != nnkStmtList:
|
|
||||||
error "expected list of arguments for help"
|
|
||||||
|
|
||||||
for n in node[1]:
|
|
||||||
expectLen n, 2
|
|
||||||
let id = n[0].strVal
|
|
||||||
var val: NimNode
|
|
||||||
case n.kind
|
|
||||||
of nnkCommand:
|
|
||||||
val =n[1]
|
|
||||||
of nnKCall:
|
|
||||||
val = n[1][0]
|
|
||||||
else: bad(n, id)
|
|
||||||
|
|
||||||
case id:
|
|
||||||
of "usage": help.usage = val
|
|
||||||
of "description": help.description = val
|
|
||||||
of "header": help.header = val
|
|
||||||
of "footer": help.footer = val
|
|
||||||
of "styles": help.styles = val
|
|
||||||
else: error "unknown help option: " & id
|
|
||||||
|
|
||||||
else: bad(node, "help")
|
|
||||||
|
|
||||||
c.help = help
|
|
||||||
|
|
||||||
func parseCliBody(body: NimNode, name = "", root = false): CliCfg =
|
func parseCliBody(body: NimNode, name = "", root = false): CliCfg =
|
||||||
result.name = name
|
result.name = name
|
||||||
result.root = root
|
result.root = root
|
||||||
for node in body:
|
# TDOO: change call to n or node?
|
||||||
if node.kind notin [nnkCall, nnkCommand, nnkPrefix]:
|
for call in body:
|
||||||
error "unexpected node kind: " & $node.kind
|
if call.kind notin [nnkCall, nnkCommand, nnkPrefix]:
|
||||||
let name = node[0].strVal
|
error "unexpected node kind: " & $call.kind
|
||||||
|
let name = call[0].strVal
|
||||||
case name:
|
case name:
|
||||||
of "name":
|
of "name":
|
||||||
expectKind node[1], nnkStrLit
|
expectKind call[1], nnkStrLit
|
||||||
result.name = node[1].strVal
|
result.name = call[1].strVal
|
||||||
of "alias":
|
of "alias":
|
||||||
if root: error "alias not supported for root command"
|
if root: error "alias not supported for root command"
|
||||||
pasrseCliAlias(result, node)
|
pasrseCliAlias(result, call)
|
||||||
of "version", "V":
|
of "version", "V":
|
||||||
result.version = node[1]
|
result.version = call[1]
|
||||||
of "usage", "?":
|
of "usage", "?":
|
||||||
result.help.usage = node[1]
|
result.usage = call[1]
|
||||||
of "...", "help":
|
of "description", "...":
|
||||||
parseCliHelp(result, node)
|
result.desc = call[1]
|
||||||
of "flags":
|
of "flags":
|
||||||
parseCliFlags(result, node[1])
|
parseCliFlags(result, call[1])
|
||||||
of "settings":
|
of "settings":
|
||||||
parseCliSettings(result, node)
|
parseCliSettings(result, call)
|
||||||
of "stopWords":
|
of "stopWords":
|
||||||
result.stopWords = parseIdentLikeList(node)
|
result.stopWords = parseIdentLikeList(call)
|
||||||
of "subcommands":
|
of "subcommands":
|
||||||
parseCliSubcommands(result, node)
|
parseCliSubcommands(result, call)
|
||||||
of "hidden":
|
of "hidden":
|
||||||
parseHiddenFlags(result, node)
|
parseHiddenFlags(result, call)
|
||||||
of "run":
|
of "run":
|
||||||
result.run = node[1]
|
result.run = call[1]
|
||||||
|
of "styles":
|
||||||
|
result.styles = call[1]
|
||||||
of "required":
|
of "required":
|
||||||
result.required = parseIdentLikeList(node)
|
result.required = parseIdentLikeList(call)
|
||||||
of "preSub":
|
of "preSub":
|
||||||
result.preSub = node[1]
|
result.preSub = call[1]
|
||||||
of "postSub":
|
of "postSub":
|
||||||
result.postSub = node[1]
|
result.postSub = call[1]
|
||||||
else:
|
else:
|
||||||
error "unknown hwylCli setting: " & name
|
error "unknown hwylCli setting: " & name
|
||||||
|
#
|
||||||
|
# for sub in result.subcommands.mitems:
|
||||||
|
# # sub.inheritFrom(result)
|
||||||
|
# sub.pre = result.preSub
|
||||||
|
# sub.post = result.postSub
|
||||||
|
|
||||||
if result.name == "":
|
if result.name == "":
|
||||||
error "missing required option: name"
|
error "missing required option: name"
|
||||||
|
|
||||||
# TODO: validate "required" flags exist here
|
# TODO: validate "required" flags exist here
|
||||||
result.addBuiltinFlags()
|
result.addBuiltinFlags()
|
||||||
|
|
||||||
if root:
|
if root:
|
||||||
propagate(result)
|
propagate(result)
|
||||||
|
|
||||||
|
@ -632,7 +574,7 @@ func subCmdsArray(cfg: CliCfg): NimNode =
|
||||||
for s in cfg.subcommands:
|
for s in cfg.subcommands:
|
||||||
let cmd = newLit(s.subName)
|
let cmd = newLit(s.subName)
|
||||||
let aliases = newLit(s.alias.mapIt("($1)" % [it]).join(" "))
|
let aliases = newLit(s.alias.mapIt("($1)" % [it]).join(" "))
|
||||||
let desc = s.help.description or newLit("")
|
let desc = s.desc or newLit("")
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
(`cmd`, `aliases`, `desc`)
|
(`cmd`, `aliases`, `desc`)
|
||||||
|
|
||||||
|
@ -646,23 +588,19 @@ func defaultUsage(cfg: CliCfg): NimNode =
|
||||||
s.add " [[[faint]flags[/]]"
|
s.add " [[[faint]flags[/]]"
|
||||||
newLit(s)
|
newLit(s)
|
||||||
|
|
||||||
func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
|
func generateCliHelperProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
|
||||||
let
|
let
|
||||||
description = cfg.help.description or newLit""
|
desc = cfg.desc or newLit("")
|
||||||
header = cfg.help.header or newLit""
|
usage = cfg.usage or defaultUsage(cfg)
|
||||||
footer = cfg.help.footer or newLit""
|
|
||||||
usage = cfg.help.usage or defaultUsage(cfg)
|
|
||||||
helpFlags = cfg.flagsArray()
|
helpFlags = cfg.flagsArray()
|
||||||
subcmds = cfg.subCmdsArray()
|
subcmds = cfg.subCmdsArray()
|
||||||
styles = cfg.help.styles or (quote do: HwylCliStyles())
|
styles = cfg.styles or (quote do: HwylCliStyles())
|
||||||
<<< usage
|
|
||||||
result = quote do:
|
result = quote do:
|
||||||
proc `printHelpName`() =
|
proc `printHelpName`() =
|
||||||
echo bb(render(newHwylCliHelp(
|
echo bb(render(newHwylCliHelp(
|
||||||
header = `header`,
|
desc = `desc`,
|
||||||
footer = `footer`,
|
|
||||||
usage = `usage`,
|
usage = `usage`,
|
||||||
description = `description`,
|
|
||||||
subcmds = `subcmds`,
|
subcmds = `subcmds`,
|
||||||
flags = `helpFlags`,
|
flags = `helpFlags`,
|
||||||
styles = `styles`,
|
styles = `styles`,
|
||||||
|
@ -876,7 +814,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
|
||||||
key = ident"key"
|
key = ident"key"
|
||||||
val = ident"val"
|
val = ident"val"
|
||||||
(longNoVal, shortNoVal) = cfg.getNoVals()
|
(longNoVal, shortNoVal) = cfg.getNoVals()
|
||||||
printHelpProc = generateCliHelpProc(cfg, printHelpName)
|
printHelperProc = generateCliHelperProc(cfg, printHelpName)
|
||||||
flagVars = setFlagVars(cfg)
|
flagVars = setFlagVars(cfg)
|
||||||
|
|
||||||
result = newTree(nnkStmtList)
|
result = newTree(nnkStmtList)
|
||||||
|
@ -951,7 +889,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
|
||||||
|
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
# block:
|
# block:
|
||||||
`printHelpProc`
|
`printHelperProc`
|
||||||
`flagVars`
|
`flagVars`
|
||||||
proc `parserProcName`(`cmdLine`: openArray[string] = commandLineParams()): seq[string] =
|
proc `parserProcName`(`cmdLine`: openArray[string] = commandLineParams()): seq[string] =
|
||||||
`parserBody`
|
`parserBody`
|
||||||
|
|
|
@ -10,6 +10,7 @@ type
|
||||||
hwylCli:
|
hwylCli:
|
||||||
name "example"
|
name "example"
|
||||||
V "0.1.0"
|
V "0.1.0"
|
||||||
|
... "a description of hwylterm"
|
||||||
flags:
|
flags:
|
||||||
[global]
|
[global]
|
||||||
color:
|
color:
|
||||||
|
|
3
todo.md
3
todo.md
|
@ -21,8 +21,7 @@
|
||||||
- [x] add support for inheriting a single flag from parent (even from a "group")
|
- [x] add support for inheriting a single flag from parent (even from a "group")
|
||||||
- [x] add support to either (lengthen commands) or provide an alias for a subcommand
|
- [x] add support to either (lengthen commands) or provide an alias for a subcommand
|
||||||
- [x] add command aliases to hwylcli help with switch
|
- [x] add command aliases to hwylcli help with switch
|
||||||
- [x] don't recreate "global"" variables in var section
|
- [ ] don't recreate "global"" variables in var section
|
||||||
- [ ] make proper test suite for cli generator
|
|
||||||
|
|
||||||
|
|
||||||
## features
|
## features
|
||||||
|
|
Loading…
Reference in a new issue