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}[/]" version bbfmt"[yellow]bbansi version[/][red] ->[/] [bold]{version}[/]"
hidden debug, testCard hidden debug, testCard
flags: flags:
debug: debug "show debug"
T bool testCard "show test card"
testCard:
T bool
style: style:
T string
? "set style for string" ? "set style for string"
- "s" - "s"
run: run:

View file

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

View file

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

View file

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