pro_args

Theo Verelst

A procedure which takes a list of {argument value} tuples preceded by a tcl procedure name and which returns a command formed of that procedure name followed by as many arguments filled in with default value or the supplied value as needed.

 proc pro_args { {p} {ar} } {
   set o {}
   set c 0; set maxc -1
   foreach a [info args $p] {
      set m {};
      foreach j $ar {
         if [string match $a [lindex $j 0]] { 
            set m 1 
            set arr [lindex $j 1]
            set maxc $c
         }
      }
      if {$m == {}} {
         if { [info default $p $a b] == 1} {
              append o " [list $b]" } {
              append o  " {}"
         }
      } {
         append o " [list $arr]"
      }
      incr c
   }
   set o "$p [lrange $o 0 $maxc]"
   return $o
 }

So if we have a procedure like this:

 proc manyargumented {{a aaaa} {b bbbb} c {d defaultford} {e e} {f f} {g gg}} {
    puts "a=$a b=$b c=$c d=$d e=$e f=$f g=$g" 
 }

We could call it and have only argument g with a different value then default by using:

 % pro_args manyargumented {{g thisisargg}}
 manyargumented aaaa bbbb {} defaultford e f thisisargg

And calling:

 % eval [pro_args manyargumented {{g thisisargg}}]
 a=aaaa b=bbbb c= d=defaultford e=e f=f g=thisisargg

For procedures with more than a few arguments it is preferable that the first arguments are the ones statistically requiring non-default arguments most the time. Then the value of pro_args is less needed, so that we can call

 % manyargumented newa newsb

But in this case the undefaulted arguments get in the way and generate an error, which pro_args would solve by:

 (Theo) 145 % pro_args manyargumented {{a newa} {b newsb} {c somec}}
 manyargumented newa newsb somec
 (Theo) 145 % eval [pro_args manyargumented {{a newa} {b newsb} {c somec}}]
 a=newa b=newsb c=somec d=defaultford e=e f=f g=gg

Of course we could use set definitions for the arguments and their defaults, and allow all kinds of set based lookups and injections to get what we want fo r procedures with a lot of arguments, and reorder during procedure definitions with an eye for the number of arguments we need to type to fullfill the procedure argument requirements.

I plan on using the pro_args or like function in an interactive command line generator, interactive command composer, which can be powerfully used for bwise blocks, and which I will extend to a interactive symbolic and graphics supported functional decomposition tool.