2013-12-16 29 views
0

Tcl命令我想生成以下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" 

回答

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"