From: Uwe Klein on
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
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
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
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
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
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: How to detect if event loop is running ?
Next: regexp