feat: add additional cli flags

This commit is contained in:
Daylin Morgan 2023-09-12 15:26:31 -05:00
parent bcc70173b5
commit 9bf3b17a7b
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -134,6 +134,7 @@ proc `&`*(x: BbString, y: BbString): Bbstring =
when isMainModule: when isMainModule:
import std/[strformat, parseopt] import std/[strformat, parseopt]
const version = staticExec "git describe --tags --always --dirty=-dev" const version = staticExec "git describe --tags --always --dirty=-dev"
const longOptPad = 8
let help = &""" let help = &"""
{bb"[bold]bbansi[/] \[[green]args...[/]] [faint][[-h,-v][/]"} {bb"[bold]bbansi[/] \[[green]args...[/]] [faint][[-h,-v][/]"}
@ -142,12 +143,16 @@ when isMainModule:
|-> {bb"[yellow] yellow text!"} |-> {bb"[yellow] yellow text!"}
bbansi "[bold red] bold red text[/] plain text..." bbansi "[bold red] bold red text[/] plain text..."
|-> {bb"[bold red] bold red text[/] plain text..."} |-> {bb"[bold red] bold red text[/] plain text..."}
bbansi "[red]some red[/red] but all bold" --style:bold
|-> {"[red]some red[/red] but all bold".bb("bold")}
flags: flags:
""" & $(bb(collect(for (s, l, d) in [ """ & $(bb(collect(for (s, l, d) in [
("h", "help", "show this help"), ("h", "help", "show this help"),
("v", "version", "show version")]: ("v", "version", "show version"),
&"[yellow]-{s}[/] [green]--{l.alignLeft(8)}[/] {d}").join("\n ") ("s", "style", "set style for string")
]:
&"[yellow]-{s}[/] [green]--{l.alignLeft(longOptPad)}[/] {d}").join("\n ")
)) ))
proc writeHelp() = proc writeHelp() =
echo help echo help
@ -155,7 +160,10 @@ flags:
proc writeVersion() = proc writeVersion() =
echo bb(&"[yellow]bbansi version[/][red] ->[/] [bold]{version}[/]") echo bb(&"[yellow]bbansi version[/][red] ->[/] [bold]{version}[/]")
quit(QuitSuccess) quit(QuitSuccess)
var strArgs: seq[string] var
strArgs: seq[string]
style: string
showDebug: bool
var p = initOptParser() var p = initOptParser()
for kind, key, val in p.getopt(): for kind, key, val in p.getopt():
case kind: case kind:
@ -164,6 +172,8 @@ flags:
case key: case key:
of "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": writeVersion() of "version", "v": writeVersion()
of "style", "s": style = val
of "debug": showDebug = true
else: else:
echo bb"[red]ERROR[/]: unexpected option/value -> ", key, ", ", val echo bb"[red]ERROR[/]: unexpected option/value -> ", key, ", ", val
echo "Option and value: ", key, ", ", val echo "Option and value: ", key, ", ", val
@ -174,4 +184,11 @@ flags:
echo help echo help
quit(QuitSuccess) quit(QuitSuccess)
for arg in strArgs: for arg in strArgs:
echo arg.bb let styled =
if style != "":
arg.bb(style)
else: arg.bb
echo styled
if showDebug:
echo debug(styled)