From dbf8f55c37f99ebc8df5c9f9483f064d31340edf Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 17 Dec 2025 16:31:15 -0300 Subject: [PATCH 1/2] Fix #93; No underscore prefix if envVarsPrefix is empty --- confutils.nim | 7 ++++++- tests/test_envvar.nim | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/confutils.nim b/confutils.nim index 16ae972..91d91d3 100644 --- a/confutils.nim +++ b/confutils.nim @@ -1043,7 +1043,12 @@ when hasSerialization: func constructEnvKey*(prefix: string, key: string): string {.raises: [].} = ## Generates env. variable names from keys and prefix following the ## IEEE Open Group env. variable spec: https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html - (prefix & "_" & key).toUpperAscii.multiReplace(("-", "_"), (" ", "_")) + let fullKey = + if prefix.len > 0: + prefix & "_" & key + else: + key + fullKey.toUpperAscii.multiReplace(("-", "_"), (" ", "_")) # On Posix there is no portable way to get the command # line from a DLL and thus the proc isn't defined in this environment. diff --git a/tests/test_envvar.nim b/tests/test_envvar.nim index a02d1bf..4f87b25 100644 --- a/tests/test_envvar.nim +++ b/tests/test_envvar.nim @@ -64,4 +64,9 @@ proc testEnvvar() = check conf.somObject.name.string == "helloObject" check conf.somObject.isNice.bool == true + test "env vars no prefix": + putEnv("DATA_DIR", "ENV VAR DATADIR") + let conf = TestConf.load(envVarsPrefix="") + check conf.dataDir.string == "ENV VAR DATADIR" + testEnvvar() From 21392b4170eacc608ec36619d46a2a2767c793fc Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 17 Dec 2025 17:13:53 -0300 Subject: [PATCH 2/2] wip --- confutils.nim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/confutils.nim b/confutils.nim index 91d91d3..da95dd7 100644 --- a/confutils.nim +++ b/confutils.nim @@ -129,7 +129,7 @@ when defined(nimscript): 100000 else: - template appInvocation: string = + proc appInvocation: string = try: getAppFilename().splitFile.name except OSError: @@ -1056,6 +1056,11 @@ func constructEnvKey*(prefix: string, key: string): string {.raises: [].} = when not declared(commandLineParams): proc commandLineParams(): seq[string] = discard +proc defaultEnvVarsPrefix(): string = + result = appInvocation() + if result.len == 0: + result = "_" + proc loadImpl[C, SecondarySources]( Configuration: typedesc[C], cmdLine = commandLineParams(), @@ -1068,7 +1073,7 @@ proc loadImpl[C, SecondarySources]( secondarySources: proc ( config: Configuration, sources: ref SecondarySources ) {.gcsafe, raises: [ConfigurationError].} = nil, - envVarsPrefix = appInvocation(), + envVarsPrefix = defaultEnvVarsPrefix(), termWidth = 0 ): Configuration {.raises: [ConfigurationError].} = ## Loads a program configuration by parsing command-line arguments @@ -1354,7 +1359,7 @@ template load*( quitOnFailure = true, ignoreUnknown = false, secondarySources: untyped = nil, - envVarsPrefix = appInvocation(), + envVarsPrefix = defaultEnvVarsPrefix(), termWidth = 0): untyped = block: let secondarySourcesRef = generateSecondarySources(Configuration)