Hello,
The goal is to parse parameters received by a nimscript used for compilation, and then modify accordingly the setup.
I use a table for that, with key => parameter and value => lambda proc which modfy the setup.
Why not a simple case/of ? Because i want to be able to have a help rule in the nimscript, by listing the keys of the table (i separate list, i could add a parameter in the case/of , and forget to add it in the help text. Anyway that would dup things).
import tables
var want_hints="on"
type fx = proc()
var params = toTable[string, fx] (
{
"--nohints": proc() = want_hints="off"
}
)
Answer from the Nim playground:
/usercode/in.nim(7, 34) Error: type mismatch: got <array[0..0, tuple of (string, proc (){.locks: 0.})]>
but expected one of:
proc (pairs: openArray[tuple of (string, fx)]): Table[system.string, in.fx]{.noSideEffect, locks: <unknown>.}
I don't understand what it means. Ok, it got an array of tuples. I understand. That's what i wanted to write, so i'm happy to see it.
I can't decode what was expected, though !
Thank you for reading.
Thank you, it works now !
Here is the complete nimscript. May be it can be helpful for someone else:
# appeler avec « nim release shooter » , « nim debug shooter » etc…
import tables
var target_release="shooter.elf"
var debug_release="shooterd.elf"
var want_hints="on"
type fx = proc() {.closure.}
# pour explication {.closure.} : https://nim-lang.org/docs/manual.html#types-procedural-type
var params = toTable[string, fx] ( {
"--nohints": proc() {.closure.} = want_hints="off"
} )
proc parse_params() =
for i in countdown( paramCount(),0):
try:
params[ paramStr(i)]()
except:
if paramStr(i)[0] == '-' :
echo "Unkwown parameter: " & paramStr(i)
quit()
parse_params()
task params, "List of parameters":
for k,v in params:
echo k
task release, "RELEASE build":
setCommand "c"
# `--` est un template pour switch
--stacktrace:off
--excessiveStackTrace:off
--linetrace:off
--debugger:off
--line_dir:off
--opt:speed
--define:release
switch( "hints", want_hints)
switch( "out", target_release)
echo "RELEASE build - terminé (" & selfExe() & ")"
task debug, "DEBUG build":
setCommand "c"
warning( "[LockLevel]", false) #--warning[LockLevel]:off
switch( "hints", want_hints)
--linedir:on
--debuginfo
--stacktrace:on
--linetrace:on
switch( "out", debug_release)
echo "DEBUG build - terminé (" & selfExe() & ")"
task clean, "Removing garbage":
for f in [target_release, debug_release]:
echo "removing " & f
rmFile( f)
Why not a simple case/of ? Because i want to be able to have a help rule in the nimscript, by listing the keys of the table (i separate list, i could add a parameter in the case/of , and forget to add it in the help text. Anyway that would dup things).
I'd use a simple case+of plus a macro to extract the help text from it. 'case' is much superior because with callbacks you're forced to make them all of the same type and you might get problems passing the data up the call chain.