Error: can't rename to "tmp_read_command": command already exists

Error: can't rename to "tmp_read_command": command already exists

考虑到您重命名命令,稍后您再次尝试重命名它,它会显示此错误消息。

Error: can't rename to "tmp_read_command": command already exists

第一次重命名而不是第二次如何重命名。我的意思是有没有一个检查条件我们可以用来查明命令是否存在。

详情:

两个文件:

include.tcl

proc snps_read_command { args } {
  echo "Hi, I am there"
  eval tmp_read_command
}
proc procedure_a { args } {
  rename -force read_command tmp_read_command
  rename -force snps_read_comamnd read_command
}

test.tcl

source ./include.tcl
procedure_a
source ./include.tcl
procedure_a

read_command
read_command

执行上面的 TCL 脚本给我这个错误信息:

Error: can't rename to "tmp_command": command already exists

这是因为我打了两次procedure_a。第一次,它重命名,第二次当它试图重命名时,它摇摇欲坠。我明白了。

问题是:

我应该写些什么才能避免第二次重命名?我尝试了信息命令 tmp_read_command 但没有成功。

is there a check condition we can put to find out if a command exists or not

最简单的检查方法之一是使用合适的模式查看info commands返回的列表的长度。这听起来比普通命令更复杂:

if {[llength [info commands tmp_read_command]]} {
    puts "The command tmp_read_command already exists"
}

对于名称中不包含 glob 元字符的命令(绝大多数命令!),这成为一种非常有效的字节码级别检查。