Compare commits

..

No commits in common. "94e5b8708819e9a3503bee686b1617912518fccb" and "e8e090c5b76bc555231bda536193df6f92a54db2" have entirely different histories.

4 changed files with 31 additions and 32 deletions

View file

@ -381,10 +381,11 @@ 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 "show debug" debug:
testCard "show test card" T bool
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 "show demo" 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 "[/" & cli.styles.flagShort & "]" result.add "[/]"
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 "[/" & cli.styles.flagLong & "]" result.add "[/]"
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 "[/" & cli.styles.flagDesc & "]" result.add "[/]"
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"
if cli.subcmds.len > 0:
result.add "\n" result.add "\n"
if cli.subcmds.len > 0:
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,7 +126,6 @@ type
type type
# ---- # ----
CliSetting* = enum CliSetting* = enum
GenerateOnly, ## Don't attach root `runProc()` node
NoHelpFlag, ## Remove the builtin help flag NoHelpFlag, ## Remove the builtin help flag
ShowHelp, ## If cmdline empty show help ShowHelp, ## If cmdline empty show help
NoNormalize ## Don't normalize flags and commands NoNormalize ## Don't normalize flags and commands
@ -245,7 +244,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"bool" result.typeNode = ident"string"
# TODO: change how this works? # TODO: change how this works?
func parseCliFlags(cfg: var CliCfg, node: NimNode) = func parseCliFlags(cfg: var CliCfg, node: NimNode) =
@ -305,21 +304,18 @@ 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 != nnkBracket: return false if node.kind == nnkPrefix:
if node.len != 1: return false if eqIdent(node[0], "---"):
if node[0].kind notin [nnkIdent, nnkStrLit]:
return false
result = true 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][0].strVal name: string = node[0][1].strVal
start = 1 start = 1
let nodeLen = node.len() let nodeLen = node.len()
@ -328,7 +324,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][0].strVal name = node[i][1].strVal
start = i + 1 start = i + 1
@ -693,7 +689,7 @@ func hwylCliImpl(cfg: CliCfg): NimNode =
# should this a CritBitTree? # should this a CritBitTree?
parserBody.add quote do: parserBody.add quote do:
var `flagSet` {.used.}: HashSet[string] var `flagSet`: HashSet[string]
parserBody.add( parserBody.add(
quote do: quote do:
@ -783,9 +779,7 @@ 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:
if GenerateOnly notin cfg.settings:
result.add quote do: result.add quote do:
`runProcName`() `runProcName`()
else: else:

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,16 +50,19 @@ 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:
auto: a:
- a T bool
? "some help" ? "some help"
b: b:
T seq[float] T seq[float]
@ -67,5 +70,6 @@ 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"{auto=}, {b=}" echo fmt"{a=}, {b=}"