mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 03:00:43 -06:00
Compare commits
No commits in common. "2a5dce888d7abf5409a62208d951ac3a4e6babfa" and "94e5b8708819e9a3503bee686b1617912518fccb" have entirely different histories.
2a5dce888d
...
94e5b87088
3 changed files with 35 additions and 71 deletions
|
@ -124,8 +124,8 @@ type
|
|||
val*: int
|
||||
|
||||
type
|
||||
# ----
|
||||
CliSetting* = enum
|
||||
# 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
|
||||
|
@ -146,12 +146,6 @@ type
|
|||
short*: char
|
||||
long*: string
|
||||
help*: NimNode
|
||||
group*: string
|
||||
|
||||
Inherit = object
|
||||
settings: set[CliSetting]
|
||||
flags: seq[string]
|
||||
groups: seq[string]
|
||||
|
||||
CliCfg = object
|
||||
stopWords*: seq[string]
|
||||
|
@ -166,9 +160,9 @@ type
|
|||
version*, usage*: NimNode
|
||||
flags*: seq[CliFlag]
|
||||
builtinFlags*: seq[BuiltinFlag]
|
||||
flagDefs*: seq[CliFlag]
|
||||
flagGroups: Table[string, seq[CliFlag]]
|
||||
required*: seq[string]
|
||||
inherit*: Inherit
|
||||
inheritFlags*: seq[string]
|
||||
root*: bool
|
||||
|
||||
# some debug procs I use to wrap my ahead aroung the magic of *macro*
|
||||
|
@ -258,35 +252,24 @@ func parseCliFlags(cfg: var CliCfg, node: NimNode) =
|
|||
var group: string
|
||||
expectKind node, nnkStmtList
|
||||
for n in node:
|
||||
<<< n
|
||||
var flag: CliFlag
|
||||
case n.kind
|
||||
of nnkCall, nnkCommand:
|
||||
flag = parseCliFlag(n)
|
||||
flag.group = group
|
||||
cfg.flagDefs.add flag
|
||||
if group == "":
|
||||
cfg.flags.add flag
|
||||
else:
|
||||
if group notin cfg.flagGroups: cfg.flagGroups[group] = @[flag]
|
||||
else: cfg.flagGroups[group].add flag
|
||||
of nnkBracket:
|
||||
group = n[0].strVal
|
||||
continue
|
||||
of nnkPrefix:
|
||||
if
|
||||
n[0].kind != nnkIdent or
|
||||
n[0].strVal != "^" or
|
||||
n.len != 2 or
|
||||
n[1].kind notin [nnkBracket, nnkIdent, nnkStrLit]:
|
||||
error "unexpected node in flags: " & $n.kind
|
||||
|
||||
case n[1].kind
|
||||
of nnkBracket:
|
||||
cfg.inherit.groups.add n[1][0].strVal
|
||||
# cfg.inheritFlags.add n[1][0].strVal
|
||||
of nnkIdent, nnkStrLit:
|
||||
cfg.inherit.flags.add n[1].strval
|
||||
else: bad(n, "flag")
|
||||
|
||||
else: bad(n, "flag")
|
||||
|
||||
cfg.flags = cfg.flagDefs.filterIt(it.group in ["", "global"])
|
||||
if n[0].kind != nnkIdent and n[0].strVal != "^":
|
||||
error "unexpected node in flags: " & $n.kind
|
||||
expectKind n[1], nnkBracket
|
||||
cfg.inheritFlags.add n[1][0].strVal
|
||||
else: bad(n, "flag")
|
||||
|
||||
func parseCliSetting(s: string): CliSetting =
|
||||
try: parseEnum[CliSetting](s)
|
||||
|
@ -349,41 +332,25 @@ func sliceStmts(node: NimNode): seq[
|
|||
start = i + 1
|
||||
|
||||
|
||||
func inheritFrom(child: var CliCfg, parent: CliCfg) =
|
||||
## inherit settings from parent command
|
||||
var
|
||||
pflags: Table[string, CliFlag]
|
||||
pgroups: Table[string, seq[CliFlag]]
|
||||
flags: seq[string]
|
||||
groups: seq[string]
|
||||
func addInheritedFlags(child: var CliCfg, parent: CliCfg, self = false) =
|
||||
let names = child.flags.mapIt(it.name)
|
||||
var groups: seq[string]
|
||||
if not self:
|
||||
groups.add child.inheritFlags
|
||||
|
||||
flags &= child.inherit.flags
|
||||
groups &= child.inherit.groups
|
||||
|
||||
for f in parent.flagDefs:
|
||||
pflags[f.name] = f
|
||||
if f.group in pgroups:
|
||||
pgroups[f.group].add f
|
||||
else:
|
||||
pgroups[f.group] = @[f]
|
||||
|
||||
if "global" in pgroups:
|
||||
# autoinherit the "global" flags
|
||||
if "global" in parent.flagGroups:
|
||||
groups.add "global"
|
||||
|
||||
for f in flags:
|
||||
if f notin pflags:
|
||||
error "expected parent command to define flag: " & f
|
||||
else:
|
||||
child.flags.add pflags[f]
|
||||
|
||||
for g in groups:
|
||||
if g notin pgroups:
|
||||
error "expected parent command to define flag group " & g
|
||||
else:
|
||||
child.flags &= pgroups[g]
|
||||
|
||||
|
||||
|
||||
if g notin parent.flagGroups:
|
||||
debugEcho parent.flagGroups.keys().toSeq()
|
||||
error "expected flag group: " & g & " to exist in parent command"
|
||||
for f in parent.flagGroups[g]:
|
||||
if f.name in names:
|
||||
error "global flag " & f.name & " conflicts with command flag"
|
||||
child.flags.add f
|
||||
|
||||
func parseCliSubcommands(cfg: var CliCfg, node: NimNode) =
|
||||
expectKind node[1], nnkStmtList
|
||||
for (name, s) in sliceStmts(node[1]):
|
||||
|
@ -392,7 +359,7 @@ func parseCliSubcommands(cfg: var CliCfg, node: NimNode) =
|
|||
nnkStmtList.newTree(node[1][s]), cfg.name & " " & name
|
||||
)
|
||||
subCfg.subName = name
|
||||
subCfg.inheritFrom(cfg)
|
||||
subCfg.addInheritedFlags(cfg)
|
||||
cfg.subcommands.add subCfg
|
||||
|
||||
func parseHiddenFlags(cfg: var CliCfg, node: NimNode) =
|
||||
|
@ -487,6 +454,8 @@ func parseCliBody(body: NimNode, name = "", root = false): CliCfg =
|
|||
sub.pre = result.preSub
|
||||
sub.post = result.postSub
|
||||
|
||||
result.addInheritedFlags(result, self = true)
|
||||
|
||||
if result.name == "":
|
||||
error "missing required option: name"
|
||||
|
||||
|
|
|
@ -16,10 +16,8 @@ hwylCli:
|
|||
yes:
|
||||
T bool
|
||||
? "set flag to yes"
|
||||
[shared]
|
||||
something:
|
||||
? "some flag only needed in one subcommand"
|
||||
config:
|
||||
[config]
|
||||
confiG:
|
||||
T seq[string]
|
||||
? "path to config file"
|
||||
* @["config.yml"]
|
||||
|
@ -33,7 +31,7 @@ hwylCli:
|
|||
the first subcommand
|
||||
|
||||
this command features both an enum flag and a Count flag
|
||||
it also inherits the `[[shared]` flag group
|
||||
it also inherits the `[[config]` flag group
|
||||
"""
|
||||
|
||||
flags:
|
||||
|
@ -44,7 +42,7 @@ hwylCli:
|
|||
T Count
|
||||
? "a count flag"
|
||||
- v
|
||||
^[shared]
|
||||
^[config]
|
||||
run:
|
||||
echo "hello from `example one` command!"
|
||||
echo args
|
||||
|
@ -60,7 +58,6 @@ hwylCli:
|
|||
and it will automatically be "bb"'ed [bold]this is bold text[/]
|
||||
"""
|
||||
flags:
|
||||
^something
|
||||
auto:
|
||||
- a
|
||||
? "some help"
|
||||
|
|
2
todo.md
2
todo.md
|
@ -18,8 +18,6 @@
|
|||
|
||||
- [ ] add support for types(metavars)/defaults/required in help output
|
||||
- [ ] add nargs to CliCfg
|
||||
- [ ] add support for inheriting a single flag from parent (even from a "group")
|
||||
- [ ] add support to either (lengthen commands) or provide an alias for a subcommand
|
||||
|
||||
|
||||
## features
|
||||
|
|
Loading…
Reference in a new issue