Running Tcl Script From Python With Arguments
I'm trying to run tcl script from python, tcl script requires command line arguments to execute, when I source the tcl file from python, it then shows the error says tclsh.eval('s
Solution 1:
The global argv variable is, apart from being set during the startup of a standard Tcl script, not special in any way. You can therefore just set it prior to doing source. In this case, doing so with lappend in a loop is probably best as it builds the structure of the variable correctly. There are two other variables that ought to be set as well (argc and argv0); overall you do it like this (as a convenient function):
defrun_tcl_script(script_name, *args):
tclsh.eval('set argv0 {{{}}}'.format(script_name))
tclsh.eval('set argv {}; set argc 0')
for a in args:
tclsh.eval('lappend argv {{{}}}; incr argc'.format(a))
tclsh.eval('source $argv0')
The {{{}}} with Python's str.format results in a single layer of braces being put around the argument, defending against most quoting issues.
Post a Comment for "Running Tcl Script From Python With Arguments"