Version 6 of Named arguments

Updated 2004-01-15 21:39:45

Richard Suchenwirth 2004-01-15 - Arguments to commands are mostly by position. But it's very easy to add the behavior known from Python or Ada (?), that arguments can be named in function calls, which documents the code a bit better, and allows any order of arguments.

The idea (as found in Welch's book) is to use an array (here called opt) keyed by argument names. Initially, you can set some default values, and possibly override them with the args of the proc (which has to be paired, i.e. contain an even number of elements):

 proc replace {s args} {
   array set opt [concat {-from 0 -to end -with ""} $args]
   string replace $s $opt(-from) $opt(-to) $opt(-with)
 }

#--- Testing:

 % replace abcdefg -from 3 -to 4 -with xx
 abcxxfg

Keywords can come in any order or be omitted (in which case the defaults apply). Flaw: "Undefined" keywords, e.g. typos (-wiht), are not detected...


Here's one which catches (but does not identify) typo's -jcw

 proc replace {s args} {
   array set opt {-from 0 -to end -with ""}
   set n [array size opt]
   array set opt $args
   if {$n != [array size opt]} { error "unknown option(s)" }
   string replace $s $opt(-from) $opt(-to) $opt(-with)
 }

RS feels prompted to reply with an error-identifying version:

 proc replace {s args} {
   array set opt {-from 0 -to end -with ""}
   foreach {key value} $args {
      if {![info exists opt($key)]} {error "bad option '$key'"}
      set opt($key) $value
   }
   string replace $s $opt(-from) $opt(-to) $opt(-with)
 }

Arts and crafts of Tcl-Tk programming