Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using optional arguments in procedures

5 views
Skip to first unread message

Roberto Hernandez

unread,
Dec 26, 2001, 7:39:40 AM12/26/01
to
Hi all,

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

Schlitz

unread,
Dec 26, 2001, 8:18:59 AM12/26/01
to
"Roberto Hernandez" <rob...@adinet.com.uy> wrote in message
news:fd8bdbb8.01122...@posting.google.com...

> 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?

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.

Paul Battersby

unread,
Dec 28, 2001, 4:59:08 PM12/28/01
to
What you are looking for is this:

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...

ulis

unread,
Dec 29, 2001, 10:42:00 AM12/29/01
to
rob...@adinet.com.uy (Roberto Hernandez) 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

Chris Nelson

unread,
Jan 2, 2002, 2:12:43 PM1/2/02
to
Roberto Hernandez wrote:
>
> Hi all,
>
> 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.
> ...

::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

0 new messages