如何设置Tcl变量的默认值?

发布于 2024-07-19 06:09:14 字数 357 浏览 4 评论 0原文

我有一些通过在命令行调用中定义变量来执行的 Tcl 脚本:

$ tclsh84 -cmd <script>.tcl -DEF<var1>=<value1> -DEF<var2>=<value2>

有没有办法检查 var1 和 var2 是否未在命令行中定义,然后为它们分配一组默认值?

我尝试了关键字 global、variable 和 set,但是当我说 "if {$==""}": "can't 时,它们都会给我这个错误读取:没有这样的变量”

I have some Tcl scripts that are executed by defining variables in the command-line invocation:

$ tclsh84 -cmd <script>.tcl -DEF<var1>=<value1> -DEF<var2>=<value2>

Is there a way to check if var1 and var2 are NOT defined at the command line and then assign them with a set of default values?

I tried the keywords global, variable, and set, but they all give me this error when I say "if {$<var1>==""}": "can't read <var1>: no such variable"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一笔一画续写前缘 2024-07-26 06:09:14

我不熟悉 tclsh 上的 -def 选项。

但是,要检查变量是否已设置,您还可以使用“info exit”而不是使用“catch”:

if { ![info exists blah] } {
  set blah default_value
}

I'm not familiar with the -def option on tclsh.

However, to check if a variable is set, instead of using 'catch', you can also use 'info exist ':

if { ![info exists blah] } {
  set blah default_value
}
深白境迁sunset 2024-07-26 06:09:14

或者,您可以使用 tcllib 中的 cmdline 包之类的东西。 这允许您设置二进制标志和名称/值参数的默认值,并为它们提供描述,以便可以显示格式化的帮助消息。 例如,如果您有一个程序需要输入文件名,以及可选的输出文件名和压缩输出的二进制选项,则可以使用类似以下内容的内容:

package require cmdline
set sUsage "Here you put a description of what your program does"
set sOptions {
    {inputfile.arg ""  "Input file name - this is required"}
    {outputfile.arg "out.txt"     "Output file name, if not given, out.txt will be used"}
    {compressoutput       "0"      "Binary flag to indicate whether the output file will be compressed"}
}

array set options [::cmdline::getoptions argv $sOptions $sUsage]
if {$options(inputfile) == ""} {puts "[::cmdline::usage $sOptions $sUsage]";exit}

.arg 后缀指示这是一个名称/值对参数,如果未列出,它将假定它是一个二进制标志。

Alternatively you can use something like the cmdline package from tcllib. This allows you to set up defaults for binary flags and name/value arguments, and give them descriptions so that a formatted help message can be displayed. For example, if you have a program that requires an input filename, and optionally an output filename and a binary option to compress the output, you might use something like:

package require cmdline
set sUsage "Here you put a description of what your program does"
set sOptions {
    {inputfile.arg ""  "Input file name - this is required"}
    {outputfile.arg "out.txt"     "Output file name, if not given, out.txt will be used"}
    {compressoutput       "0"      "Binary flag to indicate whether the output file will be compressed"}
}

array set options [::cmdline::getoptions argv $sOptions $sUsage]
if {$options(inputfile) == ""} {puts "[::cmdline::usage $sOptions $sUsage]";exit}

The .arg suffix indicates this is a name/value pair argument, if that is not listed, it will assume it is a binary flag.

汐鸠 2024-07-26 06:09:14

您可以catch您的命令以防止错误中止脚本。

if { [ catch { set foo 
lt;var1> } ] } {
    set <var1> defaultValue
}

(警告:我没有使用 TCL 解释器检查确切的语法,上面的脚本只是为了给出想法)。

You can catch your command to prevent error from aborting the script.

if { [ catch { set foo 
lt;var1> } ] } {
    set <var1> defaultValue
}

(Warning: I didn't check the exact syntax with a TCL interpreter, the above script is just to give the idea).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文