Add config helper function to find config files in a standardized manner.
This commit is contained in:
parent
b1cc4fbe51
commit
7af0acce68
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Helper functions for writing command line interfaces."
|
||||
license = "MIT"
|
||||
|
@ -1,4 +1,5 @@
|
||||
import docopt, json, nre, os, strtabs, strutils
|
||||
import std/[json, nre, os, sequtils, strtabs, strutils]
|
||||
import docopt
|
||||
|
||||
type
|
||||
CombinedConfig* = object
|
||||
@ -34,6 +35,22 @@ template walkFieldDefs*(t: NimNode, body: untyped) =
|
||||
# cfgType.walkFieldDefs:
|
||||
# let valLookup = quote do: `getVal`(
|
||||
|
||||
proc initCombinedConfig*(
|
||||
filename: string,
|
||||
docopt: Table[string, Value] = initTable[string, Value]()
|
||||
): CombinedConfig =
|
||||
|
||||
result = CombinedConfig(docopt: docopt, json: parseFile(filename))
|
||||
|
||||
proc findConfigFile*(name: string, otherLocations: seq[string] = @[]): string =
|
||||
let cfgLocations = @[
|
||||
$getEnv(name.strip(chars = {'.'}).toUpper()),
|
||||
$getEnv("HOME") / name ] & otherLocations
|
||||
|
||||
result = cfgLocations.foldl(if fileExists(b): b else: a, "")
|
||||
if result == "" or not fileExists(result):
|
||||
raise newException(ValueError, "could not find configuration file")
|
||||
|
||||
proc getVal*(cfg: CombinedConfig, key: string): string =
|
||||
let argKey = "--" & key
|
||||
let envKey = key.replace('-', '_').toUpper
|
||||
|
Loading…
x
Reference in New Issue
Block a user