Prev: How to detect if event loop is running ?
Next: regexp
From: Uwe Klein on 4 Sep 2009 04:11 miguel sofer wrote: > Jeff Godfrey wrote: > >> How about something like... set ::Defaults(procA) [ list items 22 levels 5] proc procA args { upvar ::Defaults(procA) def foreach {opt val} $def { set $opt $val } foreach opt val $args { set [ string trimleft $opt -] $val } # body of proc .... } procA -items 11 uwe
From: Alexandre Ferrieux on 4 Sep 2009 04:43 On Sep 4, 10:11 am, Uwe Klein <uwe_klein_habertw...(a)t-online.de> wrote: > > set ::Defaults(procA) [ list items 22 levels 5] > > proc procA args { > upvar ::Defaults(procA) def > > foreach {opt val} $def { > set $opt $val > } > foreach opt val $args { > set [ string trimleft $opt -] $val > } > # body of proc > .... > > } > > procA -items 11 set ::Defaults(procA) [ list -items 22 -levels 5] proc procA args { array set opt $::Defaults(procA) array set opt $args # body of proc, using $opt(-...) } -Alex
From: Uwe Klein on 4 Sep 2009 05:42 Alexandre Ferrieux wrote: > set ::Defaults(procA) [ list -items 22 -levels 5] > proc procA args { > array set opt $::Defaults(procA) > array set opt $args > # body of proc, using $opt(-...) > } > > -Alex nice. Thinking about where the downside is. uwe
From: Bruce on 4 Sep 2009 10:31 Uwe Klein wrote: > Alexandre Ferrieux wrote: >> set ::Defaults(procA) [ list -items 22 -levels 5] >> proc procA args { >> array set opt $::Defaults(procA) >> array set opt $args >> # body of proc, using $opt(-...) >> } >> >> -Alex > nice. > Thinking about where the downside is. > the one downside (and your version had it too ;) is that is silently ignores invalid options so if i typo an option name in the call e.g. procA -ietms 5 instead of an error, i just get the default value and have no idea where i screwed up. Bruce
From: Uwe Klein on 4 Sep 2009 10:47
Bruce wrote: > Uwe Klein wrote: > >> Alexandre Ferrieux wrote: >> >>> set ::Defaults(procA) [ list -items 22 -levels 5] >>> proc procA args { >>> array set opt $::Defaults(procA) >>> array set opt $args >>> # body of proc, using $opt(-...) >>> } >>> >>> -Alex >> >> nice. >> Thinking about where the downside is. >> > > the one downside (and your version had it too ;) > is that is silently ignores invalid options > so if i typo an option name in the call e.g. > > procA -ietms 5 > > instead of an error, i just get the default value > and have no idea where i screwed up. > right. my usual setup has an [if [lsearch $validopts $opt]] But I use it on occasion to hand down options to procs deeper in the stack. > Bruce uwe |