Deutsch   English   Français   Italiano  
<v5dpvv$1ecjj$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Harald Oehlmann <wortkarg3@yahoo.com>
Newsgroups: comp.lang.tcl
Subject: Re: support for built-in -opt style options for proc
Date: Tue, 25 Jun 2024 09:01:20 +0200
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <v5dpvv$1ecjj$1@dont-email.me>
References: <aF6dnUGPYfu7t-T7nZ2dnZfqnPadnZ2d@brightview.co.uk>
 <v5dn8v$1e3fd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 25 Jun 2024 09:01:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b24cd5d786704820dd35efc7fd9bdba0";
	logging-data="1520243"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/w10LAolwU4rF5l09GRiCE"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hkoNLeaWvJsvLN7BjKJFrokHtwM=
In-Reply-To: <v5dn8v$1e3fd$1@dont-email.me>
Content-Language: en-GB
Bytes: 2613

Am 25.06.2024 um 08:14 schrieb Christian Gollwitzer:
> Am 24.06.24 um 10:11 schrieb Mark Summerfield:
>> I want to create a couple of procs with these usages:
>>
>> dialog::prepare ?-parent .? ?-modal true? window
>>
> Here is another very simple argument parser: Use "dict merge".
> 
> proc someprocwithargs {args} {
>      set defaults {-parent . -modal false}
>      set options [dict merge $defaults $args]
>      if {[dict size $options] != [dict size $defaults]} {
>          return -code error "Unknown option"
>      }
>      # now use the stuff in options
> }
> 
> To extend by a mandatory argument is left as an exercise to the reader, 
> you basically take off the last element from "args". It is advisable to 
> do it this way, i.e. to make the mandatory argument positional, either 
> in the beginning - then you can simple stuff it before "args" - or at 
> the end. This way, an option can not be confused with a positional 
> argument, and no "--" stuff is needed.
> 
> There have also been LOTS of advanced implementations in pure Tcl around 
> the discussion of TIP 457 
> https://core.tcl-lang.org/tips/doc/trunk/tip/457.md
> 
>      Christian
> 

Great solution, Christian, I appreciate. I use that often, but the "dict 
size" trick is great.

dialog::prepare ?-parent .? ?-modal true? window

What magic Christian writes about "to the reader", withoput error checking:

set window [lindex $args end]
set args [lrange $args 0 end-1]

Thanks,
Harald