Sujet : Re: support for built-in -opt style options for proc
De : auriocus (at) *nospam* gmx.de (Christian Gollwitzer)
Groupes : comp.lang.tclDate : 25. Jun 2024, 07:14:55
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5dn8v$1e3fd$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
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