Add CombinedConfig#getJson to fetch config properties stored as JSON data.

This commit is contained in:
Jonathan Bernard 2022-02-10 15:35:00 -06:00
parent 4e5152bed3
commit b1cc4fbe51
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "0.7.1"
version = "0.8.0"
author = "Jonathan Bernard"
description = "Helper functions for writing command line interfaces."
license = "MIT"

View File

@ -57,6 +57,20 @@ proc getVal*(cfg: CombinedConfig, key, default: string): string =
try: return getVal(cfg, key)
except: return default
proc getJson*(cfg: CombinedConfig, key: string): JsonNode =
let argKey = "--" & key
let envKey = key.replace('-', '_').toUpper
let jsonKey = key.replace(re"(-\w)", proc (m: RegexMatch): string = ($m)[1..1].toUpper)
if cfg.docopt.contains(argKey) and cfg.docopt[argKey]: return parseJson($cfg.docopt[argKey])
elif existsEnv(envKey): return parseJson(getEnv(envKey))
elif cfg.json.hasKey(jsonKey): return cfg.json[jsonKey]
else: raise newException(ValueError, "cannot find a configuration value for \"" & key & "\"")
proc getJson*(cfg: CombinedConfig, key: string, default: JsonNode): JsonNode =
try: return getJson(cfg, key)
except: return default
proc loadEnv*(): StringTableRef =
result = newStringTable()