Suppose I have a procedure that receives many arguments. I would like
to call that procedure and just pass a couple of the arguments. Is
there a way to specify what arguments I'm passing or do I need to use
{} for all the arguments I am omitting.
Maybe an example can clarify what I mean:
------------------------------------------------------
proc foo {{argument1 ""} \
{argument2 ""} \
{argument3 ""} \
{argument4 ""} \
{argument5 ""} } {
...
}
------------------------------------------------------
If I only want to pass arguments 2 and 5, I could do this:
foo {} 2 {} {} 5
What I would like is to have a way to just specify what I'm passing
along (similar to widget command syntax) such as:
foo -argument2 2 -argument5 5
Can that be done?
BTW, can anyone recommend a good NNTP server that's also free. I have
been using "mirror.utcorp.net", which is mentioned in the Tcler's
Wiki, but it hasn't been updated for quite a few days.
TIA,
Roberto
The post to comp.lang.tcl with this title: "how do I do named arguments in
tcl when creating a procedure?" on 12/19/2001 had some good answers that
would apply here too, I believe.
proc foo {args} {
}
args is a special parameter that will contain a list of all input
parameters. So if you call it like this:
foo -argument2 2 -argument5 5
then
puts $args
will give you:
-argument2 2 -argument5 5
Paul
"Roberto Hernandez" <rob...@adinet.com.uy> wrote in message
news:fd8bdbb8.01122...@posting.google.com...
Follow the Tk way:
proc foo {args} \
{
# init some values
set val1 ""
...
foreach {opt val} $args \
{
switch -- $opt \
{
-arg1 { set val1 $val; # other }
-arg2 { # and so on }
...
default { error "bad option $opt, calling foo" }
}
}
# some usage of the values
...
}
args is a predefined parameter gathering all arguments not already collected.
ulis
::tcl::OptProc is a slow but robust and featureful solution.
Chris
--
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety. -- Benjamin Franklin