tcl/itcl - 使用两个参数对 args 输入列表进行 foreach 迭代

tcl/itcl - foreach iteration on args input list with two arguments

我编写了以下代码,应该在我的 args 输入列表上进行迭代,每次迭代都读取两个参数。它没有按照我写的方式工作的问题。我查看了 Tcl/Tk 维基百科网页,但找不到任何有用的建议。能不能按照我写的方式来做(不用转成数组)?

itcl::body class::config {args} {
if {[llength $args] > 1} {
    foreach {option value} in $args {
        if {[string length $option] == 0 || [string length $value] == 0} {
            puts "Runtime error::Bad Input: option flag or value is missing"
            return
        }
        switch --$option {
            -a { 
                if { [string is integer $value -strict] } {
                    #do something
                }
            }
            -b { 
                    if { [string is integer $value -strict] } {
                    #do something
                }
            }
        }
return }

放弃in,你只需要:

foreach {option value} $args {

请参阅 https://www.tcl.tk/man/tcl/TclCmd/foreach.htm

处的文档

一些提示。 而不是:

[string length $option] == 0

你也可以这样写:

$option == ""

在这种情况下,-strict 选项是多余的,因为您已经跳过了空字符串。此外,您应该将选项放在 "is integer":

之后
string is integer -strict $value