From 8036af6bc8d787c90c1c2a704c9091faabd63de7 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 18 Feb 2019 15:38:30 -0600 Subject: [PATCH] Add CombinedConfig.getVal that raises an exception if the config key is not found. --- cliutils.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cliutils.nim b/cliutils.nim index 31e46a3..ef7f62c 100644 --- a/cliutils.nim +++ b/cliutils.nim @@ -10,7 +10,7 @@ type TermColor = ForegroundColor or BackgroundColor -proc getVal*(cfg: CombinedConfig, key, default: string): string = +proc getVal*(cfg: CombinedConfig, key: string): string = let argKey = "--" & key let envKey = key.replace('-', '_').toUpper let jsonKey = key.replace(re"(-\w)", proc (m: RegexMatch): string = ($m)[1..1].toUpper) @@ -27,8 +27,11 @@ proc getVal*(cfg: CombinedConfig, key, default: string): string = of JNull: return "" of JObject: return $node of JArray: return $node - else: return default + else: raise newException(ValueError, "cannot find a configuration value for \"" & key & "\"") +proc getVal*(cfg: CombinedConfig, key, default: string): string = + try: return getVal(cfg, key) + except: return default proc loadEnv*(): StringTableRef = result = newStringTable()