add convience cli generator

This commit is contained in:
Daylin Morgan 2024-09-30 17:06:57 -05:00
parent 07724c4d4a
commit e447fc2b92
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 122 additions and 43 deletions

View file

@ -24,7 +24,7 @@ task docs, "Deploy doc html + search index to public/ directory":
when defined(clean):
echo fmt"clearing {deployDir}"
rmDir deployDir
for module in ["cligen", "chooser", "logging"]:
for module in ["cligen", "chooser", "logging", "cli"]:
selfExec fmt"doc --docRoot:{getCurrentDir()}/src/ --index:on --outdir:{deployDir} src/hwylterm/{module}"
selfExec fmt"doc --project --index:on {gitFlags} --outdir:{deployDir} --project src/{pkgName}.nim"
docFixup(deployDir,pkgName)

View file

@ -3,9 +3,11 @@
see also these utility modules with extra dependencies:
- [cli](./hwylterm/cli.html)
- [cligen adapter](./hwylterm/cligen.html), requires [cligen](https://github.com/c-blake/cligen)
- [chooser](./hwylterm/chooser.html)
- [logging](./hwylterm/logging.html)
]##
import hwylterm/[spin, bbansi]

View file

@ -224,33 +224,28 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} =
# NOTE: could move to standlone modules in the tools/ directory
when isMainModule:
import std/[parseopt, sugar]
import std/[parseopt]
import ./cli
const version = staticExec "git describe --tags --always --dirty=-dev"
const longOptPad = 8
const flags = collect(
for (s, l, d) in [
proc writeHelp() =
let help = $newHwylCli(
"[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-v[/]]",
"""
bbansi "[[yellow] yellow text!"
-> [yellow] yellow text![/]
bbansi "[[bold red] bold red text[[/] plain text..."
-> [bold red] bold red text[/] plain text...
bbansi "[[red]some red[[/red] but all italic" --style:italic
-> [italic][red]some red[/red] but all italic[/italic]
""",
[
("h", "help", "show this help"),
("v", "version", "show version"),
("s", "style", "set style for string"),
]:
fmt" [yellow]-{s}[/] [green]--{l.alignLeft(longOptPad)}[/] {d}"
).join("\n")
proc writeHelp() =
let help =
bbfmt"""
[bold]bbansi[/] \[[green]args...[/]] [[[faint]-h|-v[/]]
[italic]usage[/]:
bbansi "[[yellow] yellow text!"
|-> [yellow] yellow text![/]
bbansi "[[bold red] bold red text[[/] plain text..."
|-> [bold red] bold red text[/] plain text...
bbansi "[[red]some red[[/red] but all italic" --style:italic
|-> [italic][red]some red[/red] but all italic[/italic]
flags:
{flags}
"""
]
)
echo help; quit 0
proc testCard() =

View file

@ -128,24 +128,22 @@ proc choose*[T](things: openArray[T], height: Natural = 6): seq[T] =
when isMainModule:
import std/[parseopt, strformat]
func styleFlag(s, l, d: string): string =
fmt" [yellow]-{s}[/] [green]--{l.alignLeft(10)}[/] {d}"
proc writeHelp() =
const flags = [
styleFlag("h", "help", "show this help"),
styleFlag("s", "seperator", "seperator to split items"),
].join("\n")
echo bbfmt"""
[bold]hwylchoose[/] \[[green]args...[/]] \[[faint]-h[/]]
import ./cli
[italic]usage[/]:
proc writeHelp() =
let cli = newHwylCli(
"[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]",
"""
hwylchoose a b c d
hwylchoose a,b,c,d -s,
hwylchoose a,b,c,d --seperator ","
flags:
{flags}
"""
""",
[
("h", "help", "show this help"),
("s", "seperator", "seperator to split items"),
]
)
echo cli
var
posArgs: seq[string]

84
src/hwylterm/cli.nim Normal file
View file

@ -0,0 +1,84 @@
##[
# Cli
]##
import std/[strutils]
import ./bbansi
type
HwylFlag = tuple
short, long, description = ""
HwylCliStyles* = object
hdr = "b cyan"
shortFlag = "yellow"
longFlag = "magenta"
descFlag = ""
HwylCli* = object
cmd*: string
usage*: string
flags*: seq[HwylFlag]
styles*: HwylCliStyles
shortArgLen, longArgLen, descArgLen: int
func newHwylCli*(
cmd = "",
usage = "",
flags: openArray[HwylFlag] = @[],
styles = HwylCliStyles()
): HwylCli =
result.cmd = cmd
result.usage = usage
result.flags = @flags
result.styles = styles
for f in flags:
result.shortArgLen = max(result.shortArgLen, f.short.len)
result.longArgLen = max(result.longArgLen, f.long.len)
result.descArgLen = max(result.descArgLen, f.description.len)
func flagHelp(cli: HwylCli, f: HwylFlag): string =
result.add " "
if f.short != "":
result.add "[" & cli.styles.shortFlag & "]"
result.add "-" & f.short.alignLeft(cli.shortArgLen)
result.add "[/]"
else:
result.add " ".repeat(1 + cli.shortArgLen)
result.add " "
if f.long != "":
result.add "[" & cli.styles.longFlag & "]"
result.add "--" & f.long.alignLeft(cli.longArgLen)
result.add "[/]"
else:
result.add " ".repeat(2 + cli.longArgLen)
result.add " "
if f.description != "":
result.add "[" & cli.styles.descFlag & "]"
result.add f.description
result.add "[/]"
result.add "\n"
proc bbImpl(cli: HwylCli): string =
if cli.cmd != "":
result.add cli.cmd
result.add "\n"
if cli.usage != "":
result.add "\n"
result.add "[" & cli.styles.hdr & "]"
result.add "usage[/]:\n"
result.add indent(cli.usage, 2 )
result.add "\n"
if cli.flags.len > 0:
result.add "[" & cli.styles.hdr & "]"
result.add "flags[/]:\n"
for f in cli.flags:
result.add flagHelp(cli,f)
proc bb*(cli: HwylCli): BbString =
result = bb(bbImpl(cli))
proc `$`*(cli: HwylCli): string =
result = $bb(cli)