Compare commits

...

6 commits

4 changed files with 32 additions and 31 deletions

View file

@ -381,11 +381,10 @@ when isMainModule:
version bbfmt"[yellow]bbansi version[/][red] ->[/] [bold]{version}[/]"
hidden debug, testCard
flags:
debug:
T bool
testCard:
T bool
debug "show debug"
testCard "show test card"
style:
T string
? "set style for string"
- "s"
run:

View file

@ -140,9 +140,9 @@ when isMainModule:
"""
hidden demo
flags:
demo:
T bool
demo "show demo"
separator:
T string
help "separator to split items"
short "s"
run:

View file

@ -61,14 +61,14 @@ func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
if f.short != "":
result.add "[" & cli.styles.flagShort & "]"
result.add "-" & f.short.alignLeft(cli.shortArgLen)
result.add "[/]"
result.add "[/" & cli.styles.flagShort & "]"
else:
result.add " ".repeat(1 + cli.shortArgLen)
result.add " "
if f.long != "":
result.add "[" & cli.styles.flagLong & "]"
result.add "--" & f.long.alignLeft(cli.longArgLen)
result.add "[/]"
result.add "[/" & cli.styles.flagLong & "]"
else:
result.add " ".repeat(2 + cli.longArgLen)
@ -77,7 +77,7 @@ func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
if f.description != "":
result.add "[" & cli.styles.flagDesc & "]"
result.add f.description
result.add "[/]"
result.add "[/" & cli.styles.flagDesc & "]"
result.add "\n"
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
@ -101,8 +101,8 @@ func render*(cli: HwylCliHelp): string =
result.add "\n"
result.add cli.desc
result.add "\n"
result.add "\n"
if cli.subcmds.len > 0:
result.add "\n"
result.add "[" & cli.styles.header & "]"
result.add "subcommands[/]:\n"
for s in cli.subcmds:
@ -126,9 +126,10 @@ type
type
# ----
CliSetting* = enum
NoHelpFlag, ## Remove the builtin help flag
ShowHelp, ## If cmdline empty show help
NoNormalize ## Don't normalize flags and commands
GenerateOnly, ## Don't attach root `runProc()` node
NoHelpFlag, ## Remove the builtin help flag
ShowHelp, ## If cmdline empty show help
NoNormalize ## Don't normalize flags and commands
BuiltinFlag = object
name*: string
@ -244,7 +245,7 @@ func parseCliFlag(n: NimNode): CliFlag =
if result.ident == nil:
result.ident = result.name.ident
if result.typeNode == nil:
result.typeNode = ident"string"
result.typeNode = ident"bool"
# TODO: change how this works?
func parseCliFlags(cfg: var CliCfg, node: NimNode) =
@ -304,18 +305,21 @@ func parseIdentLikeList(node: NimNode): seq[string] =
func parseCliBody(body: NimNode, name: string = "", root: bool= false): CliCfg
func isSubMarker(node: NimNode): bool =
if node.kind == nnkPrefix:
if eqIdent(node[0], "---"):
result = true
if node.kind != nnkBracket: return false
if node.len != 1: return false
if node[0].kind notin [nnkIdent, nnkStrLit]:
return false
result = true
func sliceStmts(node: NimNode): seq[
tuple[name: string, slice: Slice[int]]
] =
if not isSubMarker(node[0]):
error "expected a subcommand delimiting line"
var
name: string = node[0][1].strVal
name: string = node[0][0].strVal
start = 1
let nodeLen = node.len()
@ -324,7 +328,7 @@ func sliceStmts(node: NimNode): seq[
result.add (name, start..i)
elif isSubMarker(node[i]):
result.add (name, start..(i - 1))
name = node[i][1].strVal
name = node[i][0].strVal
start = i + 1
@ -689,7 +693,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
# should this a CritBitTree?
parserBody.add quote do:
var `flagSet`: HashSet[string]
var `flagSet` {.used.}: HashSet[string]
parserBody.add(
quote do:
@ -779,9 +783,11 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
let `args` {.used.} = `parserProcName`(`cmdLine`)
`runBody`
# if cfg.root and (GenerateOnly notin cfg.settings):
if cfg.root:
result.add quote do:
`runProcName`()
if GenerateOnly notin cfg.settings:
result.add quote do:
`runProcName`()
else:
result.add quote do:
`runProcName`(`args`[1..^1])

View file

@ -26,7 +26,7 @@ hwylCli:
run:
echo "this is always run prior to subcommand parsing"
subcommands:
--- "onelonger"
[onelonger]
... """
the first subcommand
@ -50,19 +50,16 @@ hwylCli:
echo fmt"{verbose=}"
echo fmt"{config=}"
--- "two-longer"
["two-longer"]
... """
some second subcommand
a longer mulitline description that will be visible in the subcommand help
and it will automatically be "bb"'ed [bold]this is bold text[/]
"""
flags:
a:
T bool
auto:
- a
? "some help"
b:
T seq[float]
@ -70,6 +67,5 @@ hwylCli:
h "this will override the builtin 'h' for help"
run:
echo "hello from `example b` command"
echo fmt"{a=}, {b=}"
echo fmt"{auto=}, {b=}"