且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用参数从python运行tcl脚本

更新时间:1970-01-01 07:57:00

全局 argv 变量除了在标准 Tcl 脚本启动期间设置外,没有任何特殊之处.因此,您可以在执行 source 之前设置它.在这种情况下,在循环中使用 lappend 这样做可能是***的,因为它正确构建了变量的结构.还有两个其他变量也应该设置(argcargv0);总的来说,你这样做(作为一个方便的功能):

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):

def run_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')

{{{}}} 和 Python 的 str.format 导致在参数周围放置一个 层大括号,保护反对大多数引用问题.

The {{{}}} with Python's str.format results in a single layer of braces being put around the argument, defending against most quoting issues.