TCL:获取要解释为多个参数的单个变量

TCL: get single variable to be interpreted as multiple arguments

如何在 TCL 中将其中包含空格的单个字符串变量解释为多个参数?我无法更改过程定义。

这是我的意思的一个例子:

set my_options ""
if { "$some_condition" == 1 } {
    append my_options " -optionA"
}
if { "$some_other_condition" == 1 } {
    append my_options " -optionB"
}
set my_options [string trim $my_options]
not_my_proc ${my_options} ;# my_options gets interpreted as a single arg here and causes a problem:
# Flag '-optionA -optionB' is not supported by this command.

这是您使用 argument expansion 语法的地方:

not_my_proc {*}$my_options
# ..........^^^

尽管我建议使用列表而不是字符串:

  • 如果由于某种原因 my_options 字符串不是 well-formed 列表,您将看到抛出的错误
  • 如果任何选项采用 space,则列表是正确的数据结构:
set my_options [list]
lappend my_options {-option1}
lappend my_options {-option2 "with a parameter"}
not_my_proc {*}$my_options