mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2025-02-23 01:35:50 -06:00
simplify help generations
This commit is contained in:
parent
ea49ee81fc
commit
a88765e328
3 changed files with 81 additions and 20 deletions
|
@ -52,13 +52,15 @@ type
|
||||||
required* = "red"
|
required* = "red"
|
||||||
cmd* = "bold"
|
cmd* = "bold"
|
||||||
settings*: set[HwylCliStyleSetting] = {Aliases}
|
settings*: set[HwylCliStyleSetting] = {Aliases}
|
||||||
|
HwylCliLengths = object
|
||||||
|
subcmd*, subcmdDesc*, shortArg*, longArg*, descArg*, defaultVal*: int
|
||||||
|
|
||||||
HwylCliHelp* = object
|
HwylCliHelp* = object
|
||||||
header*, footer*, description*, usage*: string
|
header*, footer*, description*, usage*: string
|
||||||
subcmds*: seq[HwylSubCmdHelp]
|
subcmds*: seq[HwylSubCmdHelp]
|
||||||
flags*: seq[HwylFlagHelp]
|
flags*: seq[HwylFlagHelp]
|
||||||
styles*: HwylCliStyles
|
styles*: HwylCliStyles
|
||||||
# make 'lengths' it's own object?
|
lengths*: HwylCliLengths
|
||||||
subcmdLen*, subcmdDescLen*, shortArgLen*, longArgLen*, descArgLen*, defaultValLen*: int
|
|
||||||
|
|
||||||
# NOTE: do i need both strips?
|
# NOTE: do i need both strips?
|
||||||
func firstLine(s: string): string =
|
func firstLine(s: string): string =
|
||||||
|
@ -85,32 +87,32 @@ func newHwylCliHelp*(
|
||||||
result.usage = dedent(usage).strip()
|
result.usage = dedent(usage).strip()
|
||||||
result.flags = @flags
|
result.flags = @flags
|
||||||
result.styles = styles
|
result.styles = styles
|
||||||
# TODO: incorporate into "styles?"
|
result.lengths.subcmd = 8 # TODO: incorporate into "styles?"
|
||||||
result.subcmdLen = 8
|
|
||||||
for f in flags:
|
for f in flags:
|
||||||
result.shortArgLen = max(result.shortArgLen, f.short.len)
|
result.lengths.shortArg = max(result.lengths.shortArg, f.short.len)
|
||||||
result.longArgLen = max(result.longArgLen, f.long.len)
|
result.lengths.longArg = max(result.lengths.longArg, f.long.len)
|
||||||
result.descArgLen = max(result.descArgLen, f.description.len)
|
result.lengths.descArg = max(result.lengths.descArg, f.description.len)
|
||||||
result.defaultValLen = max(result.defaultValLen, f.defaultVal.len)
|
result.lengths.defaultVal = max(result.lengths.defaultVal, f.defaultVal.len)
|
||||||
for s in result.subcmds:
|
for s in result.subcmds:
|
||||||
result.subcmdLen = max(result.subcmdLen, s.name.len)
|
result.lengths.subcmd = max(result.lengths.subcmd, s.name.len)
|
||||||
result.subcmdDescLen = max(result.subcmdDescLen, s.desc.len)
|
result.lengths.subcmdDesc = max(result.lengths.subcmdDesc, s.desc.len)
|
||||||
|
|
||||||
func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
|
|
||||||
|
func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
|
||||||
result.add " "
|
result.add " "
|
||||||
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.lengths.shortArg)
|
||||||
result.add "[/" & cli.styles.flagShort & "]"
|
result.add "[/" & cli.styles.flagShort & "]"
|
||||||
else:
|
else:
|
||||||
result.add " ".repeat(1 + cli.shortArgLen)
|
result.add " ".repeat(1 + cli.lengths.shortArg)
|
||||||
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.lengths.longArg)
|
||||||
result.add "[/" & cli.styles.flagLong & "]"
|
result.add "[/" & cli.styles.flagLong & "]"
|
||||||
else:
|
else:
|
||||||
result.add " ".repeat(2 + cli.longArgLen)
|
result.add " ".repeat(2 + cli.lengths.longArg)
|
||||||
|
|
||||||
result.add " "
|
result.add " "
|
||||||
if f.description != "":
|
if f.description != "":
|
||||||
|
@ -127,14 +129,14 @@ func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
|
||||||
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
||||||
result.add " "
|
result.add " "
|
||||||
result.add "[" & cli.styles.cmd & "]"
|
result.add "[" & cli.styles.cmd & "]"
|
||||||
result.add subcmd.name.alignLeft(cli.subcmdLen)
|
result.add subcmd.name.alignLeft(cli.lengths.subcmd)
|
||||||
result.add "[/]"
|
result.add "[/]"
|
||||||
result.add " "
|
result.add " "
|
||||||
result.add subcmd.desc.alignLeft(cli.subcmdDescLen)
|
result.add subcmd.desc.alignLeft(cli.lengths.subcmdDesc)
|
||||||
|
|
||||||
|
|
||||||
# TODO: split this into separate procs to make overriding more fluid
|
# TODO: split this into separate procs to make overriding more fluid
|
||||||
func render*(cli: HwylCliHelp): string =
|
template render*(cli: HwylCliHelp): string =
|
||||||
var parts: seq[string]
|
var parts: seq[string]
|
||||||
|
|
||||||
if cli.header != "":
|
if cli.header != "":
|
||||||
|
@ -395,8 +397,6 @@ 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:
|
|
||||||
result.typeNode = ident"bool"
|
|
||||||
|
|
||||||
func postParse(cfg: var CliCfg) =
|
func postParse(cfg: var CliCfg) =
|
||||||
if cfg.name == "":
|
if cfg.name == "":
|
||||||
|
|
50
tests/cli/clis/customHelp.nim
Normal file
50
tests/cli/clis/customHelp.nim
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import std/[strutils,strformat, sequtils]
|
||||||
|
import hwylterm, hwylterm/hwylcli
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: actually implement this in bbansi
|
||||||
|
func stripMarkup*(s: string): string =
|
||||||
|
result = bb(s).plain
|
||||||
|
|
||||||
|
func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
|
||||||
|
result.add " "
|
||||||
|
if f.short != "":
|
||||||
|
result.add "[" & cli.styles.flagShort & "]"
|
||||||
|
result.add "-" & f.short.alignLeft(cli.lengths.shortArg)
|
||||||
|
result.add "[/" & cli.styles.flagShort & "]"
|
||||||
|
else:
|
||||||
|
result.add " ".repeat(1 + cli.lengths.shortArg)
|
||||||
|
result.add " "
|
||||||
|
|
||||||
|
let indentSize = stripMarkup(result).len
|
||||||
|
if f.long != "":
|
||||||
|
result.add "[" & cli.styles.flagLong & "]"
|
||||||
|
result.add "--" & f.long.alignLeft(cli.lengths.longArg)
|
||||||
|
result.add "[/" & cli.styles.flagLong & "]"
|
||||||
|
else:
|
||||||
|
result.add " ".repeat(2 + cli.lengths.longArg)
|
||||||
|
|
||||||
|
result.add "\n"
|
||||||
|
result.add " ".repeat(indentSize + 4)
|
||||||
|
if f.description != "":
|
||||||
|
result.add "[" & cli.styles.flagDesc & "]"
|
||||||
|
result.add f.description
|
||||||
|
result.add "[/" & cli.styles.flagDesc & "]"
|
||||||
|
if f.defaultVal != "":
|
||||||
|
result.add " "
|
||||||
|
result.add "[" & cli.styles.default & "]"
|
||||||
|
result.add "(" & f.defaultVal & ")"
|
||||||
|
result.add "[/" & cli.styles.default & "]"
|
||||||
|
|
||||||
|
hwylCli:
|
||||||
|
name "custom-help"
|
||||||
|
defaultFlagType string
|
||||||
|
flags:
|
||||||
|
input:
|
||||||
|
? "input"
|
||||||
|
* "input.txt"
|
||||||
|
output:
|
||||||
|
? "output"
|
||||||
|
* "output.txt"
|
||||||
|
run:
|
||||||
|
echo fmt"{input=}, {output=}"
|
|
@ -57,3 +57,14 @@ flags:
|
||||||
--count a count var with default
|
--count a count var with default
|
||||||
-h --help show this help""")
|
-h --help show this help""")
|
||||||
|
|
||||||
|
okWithArgs("customHelp", "--help",
|
||||||
|
"""usage:
|
||||||
|
custom-help [flags]
|
||||||
|
|
||||||
|
flags:
|
||||||
|
--input
|
||||||
|
input (input.txt)
|
||||||
|
--output
|
||||||
|
output (output.txt)
|
||||||
|
-h --help
|
||||||
|
show this help""")
|
||||||
|
|
Loading…
Add table
Reference in a new issue