Add the ability to pass a value with the -d flag

This allows the end user to use the {.magic: "IntDefine"/"StrDefine"}
pragmas to pass values into code at compile time.  This has a nice side
effect of also allowing/requiring a default value to be assigned in the
code (see osalloc.nim/StandaloneHeapSize for an example)
This commit is contained in:
Jeff Ciesielski 2016-07-04 18:11:25 -04:00
commit 94d1aa5109
7 changed files with 32 additions and 7 deletions

View file

@ -122,6 +122,13 @@ proc splitSwitch(switch: string, cmd, arg: var string, pass: TCmdLinePass,
elif switch[i] in {':', '=', '['}: arg = substr(switch, i + 1)
else: invalidCmdLineOption(pass, switch, info)
proc hasKeyValuePair(arg: string): bool =
for i in 0..arg.high:
if arg[i] in {':', '='}:
return true
return false
proc processOnOffSwitch(op: TOptions, arg: string, pass: TCmdLinePass,
info: TLineInfo) =
case whichKeyword(arg)
@ -342,7 +349,11 @@ proc processSwitch(switch, arg: string, pass: TCmdLinePass, info: TLineInfo) =
discard "allow for backwards compatibility, but don't do anything"
of "define", "d":
expectArg(switch, arg, pass, info)
defineSymbol(arg)
if hasKeyValuePair(arg):
splitSwitch(arg, key, val, pass, info)
defineSymbol(key, val)
else:
defineSymbol(arg)
of "undef", "u":
expectArg(switch, arg, pass, info)
undefSymbol(arg)