From 88572a6ce470624c0d1e80788e759a5d07d8d941 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 13 Nov 2024 09:07:54 -0600 Subject: [PATCH] add value check prior to parsing --- src/hwylterm/hwylcli.nim | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/hwylterm/hwylcli.nim b/src/hwylterm/hwylcli.nim index 060322d..5f80124 100644 --- a/src/hwylterm/hwylcli.nim +++ b/src/hwylterm/hwylcli.nim @@ -569,13 +569,21 @@ func generateCliHelperProc(cfg: CliCfg, printHelpName: NimNode): NimNode = styles = `styles`, ))) +proc preParseCheck(key: string, val: string) = + if val == "": + hwylCliError( + "expected value for flag: [b]" & key + ) + proc parse*(p: OptParser, key: string, val: string, target: var bool) = target = true proc parse*(p: OptParser, key: string, val: string, target: var string) = + preParseCheck(key, val) target = val proc parse*(p: OptParser, key: string, val: string, target: var int) = + preParseCheck(key, val) try: target = parseInt(val) except: @@ -591,15 +599,17 @@ macro enumNames(a: typed): untyped = result.add newLit ai.strVal proc parse*[E: enum](p: OptParser, key: string, val: string, target: var E) = + preParseCheck(key, val) try: target = parseEnum[E](val) except: let choices = enumNames(E).join(",") hwylCliError( - "failed to parse value for [b]" & key & "[/] as enum: [b]" & val & "[/], expected one of: " & choices + "failed to parse value for [b]" & key & "[/] as enum: [b]" & val & "[/] expected one of: " & choices ) proc parse*(p: OptParser, key: string, val: string, target: var float) = + preParseCheck(key, val) try: target = parseFloat(val) except: @@ -608,6 +618,7 @@ proc parse*(p: OptParser, key: string, val: string, target: var float) = ) proc parse*[T](p: OptParser, key: string, val: string, target: var seq[T]) = + preParseCheck(key, val) var parsed: T parse(p, key, val, parsed) target.add parsed