generalize error proc

This commit is contained in:
Daylin Morgan 2024-11-06 22:32:31 -06:00
parent 22fae9ba6a
commit a4e0f3fc7b
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -448,9 +448,15 @@ func subCmdsArray(cfg: CliCfg): NimNode =
result.add quote do: result.add quote do:
(`cmd`, `desc`) (`cmd`, `desc`)
proc hwylCliError(msg: string | BbString) =
quit $(bb("error ", "red") & bb(msg))
func defaultUsage(cfg: CliCfg): NimNode = func defaultUsage(cfg: CliCfg): NimNode =
newLit("[b]" & cfg.name & "[/]" & " [[[faint]-h[/]]") var s = "[b]" & cfg.name & "[/]"
if cfg.subcommands.len > 0:
s.add " [bold italic]subcmd[/]"
s.add " [[[faint]flags[/]]"
newLit(s)
func generateCliHelperProc(cfg: CliCfg, printHelpName: NimNode): NimNode = func generateCliHelperProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
let let
@ -476,18 +482,20 @@ func generateCliHelperProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
proc checkVarSet[T](name: string, target: T) = proc checkVarSet[T](name: string, target: T) =
var default: T var default: T
if target == default: if target == default:
quit($bb("[red]error[/]: missing required flag: [b]" & name)) hwylCliError("missing required flag: [b]" & name)
proc checkDefaultExists[T](target: T, key: string, val: string) = proc checkDefaultExists[T](target: T, key: string, val: string) =
var default: T var default: T
if target == default and val == "": if target == default and val == "":
quit($bb("[red]error[/]: expected value for: [b]" & key)) hwylCliError("expected value for: [b]" & key)
proc tryParseInt(key: string, val: string): int = proc tryParseInt(key: string, val: string): int =
try: try:
result = parseInt(val) result = parseInt(val)
except: except:
quit($bb("[red]error[/]: failed to parse value for [b]" & key & "[/] as integer: [b]" & val)) hwylCliError(
"failed to parse value for [b]" & key & "[/] as integer: [b]" & val
)
func addOrOverwrite[T](target: var seq[T], default: seq[T], val: T) = func addOrOverwrite[T](target: var seq[T], default: seq[T], val: T) =
if target != default: if target != default:
@ -495,7 +503,6 @@ func addOrOverwrite[T](target: var seq[T], default: seq[T], val: T) =
else: else:
target = @[val] target = @[val]
func assignField(f: CliFlag): NimNode = func assignField(f: CliFlag): NimNode =
let key = ident"key" let key = ident"key"
let varName = ident(f.ident) let varName = ident(f.ident)
@ -534,7 +541,7 @@ func assignField(f: CliFlag): NimNode =
func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): NimNode = func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): NimNode =
var caseStmt = nnkCaseStmt.newTree(ident("key")) var caseStmt = nnkCaseStmt.newTree(ident("key"))
caseStmt.add nnkOfBranch.newTree(newLit(""), quote do: quit "empty flag not supported currently") caseStmt.add nnkOfBranch.newTree(newLit(""), quote do: hwylCliError("empty flag not supported currently"))
if NoHelpFlag notin cfg.settings: if NoHelpFlag notin cfg.settings:
caseStmt.add nnkOfBranch.newTree( caseStmt.add nnkOfBranch.newTree(
@ -558,7 +565,7 @@ func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): N
branch.add assignField(f) branch.add assignField(f)
caseStmt.add branch caseStmt.add branch
caseStmt.add nnkElse.newTree(quote do: quit "unknown flag: " & key) caseStmt.add nnkElse.newTree(quote do: hwylCliError("unknown flag: [b]" & key))
result = nnkStmtList.newTree(caseStmt) result = nnkStmtList.newTree(caseStmt)
@ -659,7 +666,7 @@ func hwylCliImpl(cfg: CliCfg, root = false): NimNode =
# , # ,
nnkCaseStmt.newTree( nnkCaseStmt.newTree(
kind, kind,
nnkOfBranch.newTree(ident("cmdError"), quote do: quit($(bb"[red]cli error[/]: " & p.message), 1)), nnkOfBranch.newTree(ident("cmdError"), quote do: hwylCliError(p.message)),
nnkOfBranch.newTree(ident("cmdEnd"), quote do: assert false), nnkOfBranch.newTree(ident("cmdEnd"), quote do: assert false),
# TODO: add nArgs to change how cmdArgument is handled ... # TODO: add nArgs to change how cmdArgument is handled ...
nnkOfBranch.newTree(ident("cmdArgument"), quote do: result.add `key`), nnkOfBranch.newTree(ident("cmdArgument"), quote do: result.add `key`),
@ -695,7 +702,7 @@ func hwylCliImpl(cfg: CliCfg, root = false): NimNode =
var handleSubCommands = nnkStmtList.newTree() var handleSubCommands = nnkStmtList.newTree()
handleSubCommands.add quote do: handleSubCommands.add quote do:
if `args`.len == 0: if `args`.len == 0:
quit "expected subcommand" hwylCliError("expected subcommand")
var subCommandCase = nnkCaseStmt.newTree( var subCommandCase = nnkCaseStmt.newTree(
quote do: `args`[0] quote do: `args`[0]
@ -708,7 +715,7 @@ func hwylCliImpl(cfg: CliCfg, root = false): NimNode =
subcommandCase.add nnkElse.newTree( subcommandCase.add nnkElse.newTree(
quote do: quote do:
quit $bb"[red]error[/] unknown subcommand: " & `args`[0] hwylCliError("unknown subcommand" & `args`[0])
) )
runBody.add handleSubCommands.add subCommandCase runBody.add handleSubCommands.add subCommandCase