mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2025-02-23 09:45:50 -06:00
auto usage
This commit is contained in:
parent
ac3e5663d0
commit
717ca3a3ec
2 changed files with 33 additions and 12 deletions
|
@ -1,9 +1,10 @@
|
||||||
import std/[os, strformat, strutils]
|
import std/os except getCurrentDir
|
||||||
|
import std/[strformat, strutils]
|
||||||
|
|
||||||
task test, "run tests":
|
task test, "run tests":
|
||||||
selfExec "r tests/tbbansi.nim"
|
selfExec "r tests/tbbansi.nim"
|
||||||
selfExec "r tests/tcli.nim"
|
selfExec "r tests/tcli.nim"
|
||||||
selfExec "r tests/cli/tcli.nim"
|
selfExec "r tests/cli/tester.nim"
|
||||||
|
|
||||||
task develop, "install cligen for development":
|
task develop, "install cligen for development":
|
||||||
exec "nimble install -l 'cligen@1.7.5'"
|
exec "nimble install -l 'cligen@1.7.5'"
|
||||||
|
|
|
@ -654,6 +654,10 @@ func parseCliHelp(c: var CliCfg, node: NimNode) =
|
||||||
func badNode(c: CliCfg, node: NimNode, msg: string) =
|
func badNode(c: CliCfg, node: NimNode, msg: string) =
|
||||||
c.err "unexpected node kind: " & $node.kind & "\n" & msg
|
c.err "unexpected node kind: " & $node.kind & "\n" & msg
|
||||||
|
|
||||||
|
func isSeq(arg: CliArg): bool =
|
||||||
|
# NOTE: does this need to be more rigorous?
|
||||||
|
arg.typeNode.kind == nnkBracketExpr
|
||||||
|
|
||||||
func parseCliArg(c: CliCfg, node: NimNode): CliArg =
|
func parseCliArg(c: CliCfg, node: NimNode): CliArg =
|
||||||
expectLen node, 2
|
expectLen node, 2
|
||||||
result.name = node[0].strVal
|
result.name = node[0].strVal
|
||||||
|
@ -780,6 +784,14 @@ func defaultUsage(cfg: CliCfg): NimNode =
|
||||||
var s = "[b]" & cfg.name & "[/]"
|
var s = "[b]" & cfg.name & "[/]"
|
||||||
if cfg.subcommands.len > 0:
|
if cfg.subcommands.len > 0:
|
||||||
s.add " [bold italic]subcmd[/]"
|
s.add " [bold italic]subcmd[/]"
|
||||||
|
if cfg.args.len > 0:
|
||||||
|
for arg in cfg.args:
|
||||||
|
s.add " [bold italic]"
|
||||||
|
s.add arg.name
|
||||||
|
if arg.isSeq:
|
||||||
|
s.add "..."
|
||||||
|
|
||||||
|
s.add"[/]"
|
||||||
s.add " [[[faint]flags[/]]"
|
s.add " [[[faint]flags[/]]"
|
||||||
newLit(s)
|
newLit(s)
|
||||||
|
|
||||||
|
@ -991,12 +1003,16 @@ type
|
||||||
First, ## First positional uses seq[[T]]
|
First, ## First positional uses seq[[T]]
|
||||||
Last, ## Last positional uses seq[[T]]
|
Last, ## Last positional uses seq[[T]]
|
||||||
|
|
||||||
|
|
||||||
func getMultiArgKind(cfg: CliCfg): MultiArgKind =
|
func getMultiArgKind(cfg: CliCfg): MultiArgKind =
|
||||||
if cfg.args.len == 1:
|
if cfg.args.len == 1:
|
||||||
|
if cfg.args[0].isSeq:
|
||||||
return First
|
return First
|
||||||
if cfg.args[0].typeNode.kind == nnkBracketExpr:
|
else:
|
||||||
|
return NoMulti
|
||||||
|
if cfg.args[0].isSeq:
|
||||||
return First
|
return First
|
||||||
if cfg.args[^1].typeNode.kind == nnkBracketExpr:
|
if cfg.args[^1].isSeq:
|
||||||
return Last
|
return Last
|
||||||
|
|
||||||
func parseArgs(p: OptParser, target: var string) =
|
func parseArgs(p: OptParser, target: var string) =
|
||||||
|
@ -1085,19 +1101,23 @@ func genPosArgHandler(cfg: CliCfg, body: NimNode) =
|
||||||
body.add quote do:
|
body.add quote do:
|
||||||
parseArgs(result[`i`], `namedArg`)
|
parseArgs(result[`i`], `namedArg`)
|
||||||
|
|
||||||
# clear out 'args'
|
|
||||||
if ExactArgs in cfg.settings:
|
if ExactArgs in cfg.settings:
|
||||||
if maKind == NoMulti:
|
if maKind == NoMulti:
|
||||||
body.add quote do:
|
body.add quote do:
|
||||||
result = @[(`numArgs`)..^1]
|
result = result[(`numArgs`)..^1]
|
||||||
else:
|
if result.len > 0:
|
||||||
body.add quote do:
|
hwylCliError("unexpected positional arguments: " & $result)
|
||||||
result = @[`numArgs`..^1]
|
# # here if result.len > 1 then it should error?
|
||||||
|
# # really if we pass ExactArgs the parse function should return nothing...
|
||||||
|
|
||||||
# first and last already absorbed the remaining args
|
# first and last already absorbed the remaining args
|
||||||
elif maKind in [First, Last]:
|
# so ExactArgs is a NOOP use a compile Hint?
|
||||||
|
|
||||||
|
if maKind in [First, Last]:
|
||||||
|
if ExactArgs in cfg.settings:
|
||||||
|
hint "Exact args is a No-op when one of the positional args is seq[T]"
|
||||||
body.add quote do:
|
body.add quote do:
|
||||||
result = @[] # args are ab
|
result = @[]
|
||||||
|
|
||||||
func addPostParseHook(cfg: CliCfg, body: NimNode) =
|
func addPostParseHook(cfg: CliCfg, body: NimNode) =
|
||||||
## generate block to set defaults and check for required flags
|
## generate block to set defaults and check for required flags
|
||||||
|
|
Loading…
Add table
Reference in a new issue