diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-10-30 17:21:05 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-10-30 17:21:05 +0100 |
commit | 08950649839c1df1504d495fccfea04bf11f55ed (patch) | |
tree | 8d56f635aec69802506b2e8498366e05140937d2 | |
parent | a155130cf2a1ba57501f5b22e4b6818fbc2bd0ae (diff) | |
download | Nim-08950649839c1df1504d495fccfea04bf11f55ed.tar.gz |
getEnv now supports a 'default' parameter; refs #6019
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | compiler/scriptconfig.nim | 2 | ||||
-rw-r--r-- | compiler/vmops.nim | 7 | ||||
-rw-r--r-- | lib/pure/includes/osenv.nim | 4 | ||||
-rw-r--r-- | lib/system/nimscript.nim | 2 |
5 files changed, 12 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md index f8ad88842..7a3cbd5e7 100644 --- a/changelog.md +++ b/changelog.md @@ -37,3 +37,5 @@ This means its results are more precise and it can't run into an infinite loop anymore. - Added ``typetraits.$`` as an alias for ``typetraits.name``. +- ``os.getEnv`` now takes an optional ``default`` parameter that tells ``getEnv`` + what to return if the environment variable does not exist. diff --git a/compiler/scriptconfig.nim b/compiler/scriptconfig.nim index 0c31eadbe..22377a1e2 100644 --- a/compiler/scriptconfig.nim +++ b/compiler/scriptconfig.nim @@ -79,7 +79,7 @@ proc setupVM*(module: PSym; cache: IdentCache; scriptName: string; setResult(a, osproc.execCmd getString(a, 0)) cbconf getEnv: - setResult(a, os.getEnv(a.getString 0)) + setResult(a, os.getEnv(a.getString 0, a.getString 1)) cbconf existsEnv: setResult(a, os.existsEnv(a.getString 0)) cbconf dirExists: diff --git a/compiler/vmops.nim b/compiler/vmops.nim index 38135951d..2a00f207a 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -47,6 +47,11 @@ template wrap1s_ospaths(op) {.dirty.} = setResult(a, op(getString(a, 0))) ospathsop op +template wrap2s_ospaths(op) {.dirty.} = + proc `op Wrapper`(a: VmArgs) {.nimcall.} = + setResult(a, op(getString(a, 0), getString(a, 1))) + ospathsop op + template wrap1s_system(op) {.dirty.} = proc `op Wrapper`(a: VmArgs) {.nimcall.} = setResult(a, op(getString(a, 0))) @@ -96,7 +101,7 @@ proc registerAdditionalOps*(c: PCtx) = wrap1f_math(ceil) wrap2f_math(fmod) - wrap1s_ospaths(getEnv) + wrap2s_ospaths(getEnv) wrap1s_ospaths(existsEnv) wrap1s_os(dirExists) wrap1s_os(fileExists) diff --git a/lib/pure/includes/osenv.nim b/lib/pure/includes/osenv.nim index 8d2fc235a..ae62a5c4e 100644 --- a/lib/pure/includes/osenv.nim +++ b/lib/pure/includes/osenv.nim @@ -94,7 +94,7 @@ proc findEnvVar(key: string): int = if startsWith(environment[i], temp): return i return -1 -proc getEnv*(key: string): TaintedString {.tags: [ReadEnvEffect].} = +proc getEnv*(key: string, default = ""): TaintedString {.tags: [ReadEnvEffect].} = ## Returns the value of the `environment variable`:idx: named `key`. ## ## If the variable does not exist, "" is returned. To distinguish @@ -108,7 +108,7 @@ proc getEnv*(key: string): TaintedString {.tags: [ReadEnvEffect].} = return TaintedString(substr(environment[i], find(environment[i], '=')+1)) else: var env = c_getenv(key) - if env == nil: return TaintedString("") + if env == nil: return TaintedString(default) result = TaintedString($env) proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} = diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index f5b9cf3ed..f91dae41e 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -106,7 +106,7 @@ proc cmpic*(a, b: string): int = ## Compares `a` and `b` ignoring case. cmpIgnoreCase(a, b) -proc getEnv*(key: string): string {.tags: [ReadIOEffect].} = +proc getEnv*(key: string; default = ""): string {.tags: [ReadIOEffect].} = ## Retrieves the environment variable of name `key`. builtin |