# ------------------------------------------------------- # proc: # easyargs -just a piece of demo code # # args: # a # b # # switches: # -up # -down # -in # -out # # ------------------------------------------------------- proc easyargs {a b args} { # read in args, set values according to switch names foreach {opt val} $args { set tmp [string trim $opt -] set $tmp $val } #just to prove that the values are set... foreach item {up down in out} { puts "$item [set $item]" } } easyargs red green -up 0 -down 1 -in 0 -out 1WJG (16/12/11) I thought that I'd revisit this task but allow for the setting of defaults and some error checking. The above is fine, but I don't want to have such a bulky solution. Much better to have a single proc to call to handle the necessaries.
#--------------- # parse options string and set values within scope of calling procedure #--------------- # Arguments # options array containing procedure option names and default values # args list of options and values passed to calling procedure # Returns # 0 if successful or -1 if there is an error. # # Notes # The function creates variables within the scope of the calling function # by stripping away the leading "-" and using a variable name derived from # shifting the option string to lowercase. # # If an undefined option is specified, the error will be reported to stderr # and the script will terminate. # # Following the creation of the variables, the options array will be unset. # proc parseOptions {options args} { upvar $options opts set names [array names opts] # set default values for the options foreach item $names { set aa [string tolower [string trimleft $item -]] uplevel 1 set $aa $opts($item) } # change option values to those specified in args foreach {a b} [lindex $args 0] { if { [string first $a $names] >= 0 } { set aa [string tolower [string trimleft $a -]] uplevel 1 set $aa [list $b] } else { set err "ERROR! Invalid option $a, should be one of [array names opts]." exit -1 } } # clear the options array uplevel 1 unset opts } proc testProc1 {val args} { # set options and default values array set opts {-opt1 A -opt2 B -opt3 C -opt4 ZZZZZ} puts [parray opts] parseOptions opts $args foreach i [info vars] { puts "$i = [set $i]" } if { [catch {puts [parray opts] }] } { puts "Array opts no longer exists" } } proc testProc2 {val args} { # set options and default values array set opts {-Day A -Day B -Month C -Year D } parseOptions opts $args foreach i [info vars] { puts "$i = [set $i]" } puts "Happy Yuletide: $day $month $year" } testProc1 25-DEC -opt1 "HAPPY NEW YEAR" -opt2 "Merry Christmas" -opt3 "MERRY YULETIDE" testProc2 25-DEC -Day 25th -Month December -Year 2011
See also Named arguments