Search code examples
if-statementtclexpandbraces

using TCL expand in if statement


I try to use an expand of a list of arguments ($options) in an if-statement in tcl

if {! [runCommandInRepo $componentpath git init {*}$options] } { exit 1 }

but I get an error saying there are "extra characters after close-brace"

How do you expand a list inside an if statement


Solution

  • It sounds like you're using a very old version of Tcl there, probably 8.4 or before. The expansion syntax was added in Tcl 8.5. (8.4 is no longer supported, FYI.)

    The “fix” is to use eval carefully:

    if {! [eval [list runCommandInRepo $componentpath git init] [lrange $options 0 end]] } { exit 1 }
    

    Yes, that is eval [list …] [lrange … 0 end]. That guarantees that everything is de-fanged of all possible failure modes (or at least that they'll reliably generate a nice error message telling you exactly what the problem is).

    But really, upgrade please!