1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
namespace eval ::dictargs {}
if {[info commands ::dictargs::parse] eq {}} {
proc ::dictargs::parse {argdef argdict} {
set result {}
dict for {field info} $argdef {
if {![string is alnum [string index $field 0]]} {
error "$field is not a simple variable name"
}
upvar 1 $field _var
set aliases {}
if {[dict exists $argdict $field]} {
set _var [dict get $argdict $field]
continue
}
if {[dict exists $info aliases:]} {
set found 0
foreach {name} [dict get $info aliases:] {
if {[dict exists $argdict $name]} {
set _var [dict get $argdict $name]
set found 1
break
}
}
if {$found} continue
}
if {[dict exists $info default:]} {
set _var [dict get $info default:]
continue
}
set mandatory 1
if {[dict exists $info mandatory:]} {
set mandatory [dict get $info mandatory:]
}
if {$mandatory} {
error "$field is required"
}
}
}
}
###
# Named Procedures as new command
###
proc ::dictargs::proc {name argspec body} {
set result {}
append result "::dictargs::parse \{$argspec\} \$args" \;
append result $body
uplevel 1 [list ::proc $name [list [list args [list dictargs $argspec]]] $result]
}
proc ::dictargs::method {name argspec body} {
set class [lindex [::info level -1] 1]
set result {}
append result "::dictargs::parse \{$argspec\} \$args" \;
append result $body
oo::define $class method $name [list [list args [list dictargs $argspec]]] $result
}
|