在 tcl 过程中设置 2 个默认变量

Set 2 default variables in a tcl procedure

当在 tcl 中定义如下所示的过程时,如何调用仅定义 a 和 c 的过程?有什么办法吗?

proc test1 { a {b 2} {c 3} } {
   puts "$a $b $c"
}

这是一种技术,比您希望的要复杂一些,但不会太混乱:

proc test1 { args } {
    # set the default values
    array set values {b 2 c 3}

    # todo: validate that $args is a list with an even number of items

    # now merge in the args
    array set values $args

    # and do stuff with the values ...
    parray values
}

test1 a 10 c 14

您有时会看到应用程序使用这种技术,其中数组键有一个前导破折号,看起来像选项:

proc test1 args {
    array set values {-b 2 -c 3}
    array set values $args
    parray values
}

test1 -a 10 -c 14

谢谢 Glenn 和 Peter,我加入了你们的帖子,我得到了

proc test1 { a args } {
    array set valores [list a $a  -b 2 -c 3]
    array set valores $args
    puts "$valores(a) $valores(-b) $valores(-c)" 
}

这解决了我想要的问题。

现在我可以打电话了

> proc 12 -c 8
> 12 2 8