我想生成以下tcl命令,参数以树结构传递
proc find { args } {
array set opt {-do "" -delete ""}
foreach {key value} $args {
if {![info exists opt($key)]} {
error "Wrong type '$key' The argument must have "
}
set opt($key) $value
}
if {$opt({-do) == ""} {
....
if {$opt(-value ) == ""} {
..........
if {$opt(-set ) == ""} {
..........
}
}
}
if {$opt({-delete) == ""} {
}
}
proc -do "I1"
proc -do "I1" -value "t" #print this results only
proc -do "I1" -value "t" -set "A" #should not touch function { proc -do "I1" -value "" }(print this results only)
proc -delete "all"
proc -delete "all" -select "I4"
答案 0 :(得分:1)
我只会合并用户的参数,然后再验证:
proc find { args } {
array set opt $args ;# user
# now figure out how to dispatch:
if {$opt(-do) ne "" &&
[info exists opt(-value)] && $opt(-value) ne "" &&
[info exists opt(-set)] && $opt(-set) ne ""
} {
set_value $opt(-do) $opt(-value) $opt(-set)
} elseif {$opt(-delete) ne ""} {
delete $opt(-delete)
} else {
display_hierarchy
}
}
proc set_value {key value set} {
# ...
}
proc delete {key} {
# ...
}
prod display_hierarchy {} {
# ...
}
注意,puts
需要一个字符串来打印(http://tcl.tk/man/tcl8.5/TclCmd/puts.htm),所以
以下是语法错误
puts -value "t" -set "A"