mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 03:00:43 -06:00
add convience cli generator
This commit is contained in:
parent
07724c4d4a
commit
e447fc2b92
5 changed files with 122 additions and 43 deletions
|
@ -24,7 +24,7 @@ task docs, "Deploy doc html + search index to public/ directory":
|
||||||
when defined(clean):
|
when defined(clean):
|
||||||
echo fmt"clearing {deployDir}"
|
echo fmt"clearing {deployDir}"
|
||||||
rmDir 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 --docRoot:{getCurrentDir()}/src/ --index:on --outdir:{deployDir} src/hwylterm/{module}"
|
||||||
selfExec fmt"doc --project --index:on {gitFlags} --outdir:{deployDir} --project src/{pkgName}.nim"
|
selfExec fmt"doc --project --index:on {gitFlags} --outdir:{deployDir} --project src/{pkgName}.nim"
|
||||||
docFixup(deployDir,pkgName)
|
docFixup(deployDir,pkgName)
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
|
|
||||||
see also these utility modules with extra dependencies:
|
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)
|
- [cligen adapter](./hwylterm/cligen.html), requires [cligen](https://github.com/c-blake/cligen)
|
||||||
- [chooser](./hwylterm/chooser.html)
|
- [chooser](./hwylterm/chooser.html)
|
||||||
- [logging](./hwylterm/logging.html)
|
- [logging](./hwylterm/logging.html)
|
||||||
|
|
||||||
]##
|
]##
|
||||||
|
|
||||||
import hwylterm/[spin, bbansi]
|
import hwylterm/[spin, bbansi]
|
||||||
|
|
|
@ -224,33 +224,28 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} =
|
||||||
|
|
||||||
# NOTE: could move to standlone modules in the tools/ directory
|
# NOTE: could move to standlone modules in the tools/ directory
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import std/[parseopt, sugar]
|
import std/[parseopt]
|
||||||
|
import ./cli
|
||||||
|
|
||||||
const version = staticExec "git describe --tags --always --dirty=-dev"
|
const version = staticExec "git describe --tags --always --dirty=-dev"
|
||||||
const longOptPad = 8
|
|
||||||
const flags = collect(
|
proc writeHelp() =
|
||||||
for (s, l, d) in [
|
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"),
|
("h", "help", "show this help"),
|
||||||
("v", "version", "show version"),
|
("v", "version", "show version"),
|
||||||
("s", "style", "set style for string"),
|
("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
|
echo help; quit 0
|
||||||
|
|
||||||
proc testCard() =
|
proc testCard() =
|
||||||
|
|
|
@ -128,24 +128,22 @@ proc choose*[T](things: openArray[T], height: Natural = 6): seq[T] =
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import std/[parseopt, strformat]
|
import std/[parseopt, strformat]
|
||||||
func styleFlag(s, l, d: string): string =
|
import ./cli
|
||||||
fmt" [yellow]-{s}[/] [green]--{l.alignLeft(10)}[/] {d}"
|
|
||||||
proc writeHelp() =
|
proc writeHelp() =
|
||||||
const flags = [
|
let cli = newHwylCli(
|
||||||
styleFlag("h", "help", "show this help"),
|
"[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]",
|
||||||
styleFlag("s", "seperator", "seperator to split items"),
|
"""
|
||||||
].join("\n")
|
hwylchoose a b c d
|
||||||
echo bbfmt"""
|
hwylchoose a,b,c,d -s,
|
||||||
[bold]hwylchoose[/] \[[green]args...[/]] \[[faint]-h[/]]
|
hwylchoose a,b,c,d --seperator ","
|
||||||
|
""",
|
||||||
[italic]usage[/]:
|
[
|
||||||
hwylchoose a b c d
|
("h", "help", "show this help"),
|
||||||
hwylchoose a,b,c,d -s,
|
("s", "seperator", "seperator to split items"),
|
||||||
hwylchoose a,b,c,d --seperator ","
|
]
|
||||||
|
)
|
||||||
flags:
|
echo cli
|
||||||
{flags}
|
|
||||||
"""
|
|
||||||
|
|
||||||
var
|
var
|
||||||
posArgs: seq[string]
|
posArgs: seq[string]
|
||||||
|
|
84
src/hwylterm/cli.nim
Normal file
84
src/hwylterm/cli.nim
Normal 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)
|
Loading…
Reference in a new issue