Is it possible to "push" new command line style defines (e.g. -d:someSwitch in code?
I've tried search but couldn't find anything that would let me do when defined(someSwitch) in normal Nim code. It doesn't help that define is a rather generic search term. I know there's the push and pop pragma's but they seem to only work on pre-defined defines.
Mainly I'm curious if it's possible for usage in testing for example to do things like when defined(testing) that could be set in unit tests or via the command line for manual testing. I know it's possible to use const for some cases, or {.booldefine.} but then you need to always import or define the constants in each new module. Whereas when defined(someWeirdCase) let's it be more adhoc and would make it nicer to be able to invoke in a unit test, or compile time flag setting.
Is config.nims the only way to handle setting these sort of adhoc define flags?
This works for me:
when defined(myVar):
echo "defined"
else:
echo "not defined"
Compiling it with nim c -d:myvar test.nim and executing, prints defined, when compiling with nim c test.nim and executing, prints not defined.
Compiling it with nim c -d:myvar test.nim and executing, prints defined, when compiling with nim c test.nim and executing, prints not defined.
Yah that works, but what I'm asking is if I can do the -d:myvar in another Nim module. Say in a unit test do:
# tests.nim
{.push define(myVar).}
# now test code relying on `when defined(myVar)`
It's also easy to do in a config.nims file but affect all the Nim files in the directory.
It's also easy to do in a config.nims file but affect all the Nim files in the directory.
Naming with filename.nims should affect only the filename.nim . At least it works like that when I tried compiling/running a single module file.