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

argc and argv variables

1 view
Skip to first unread message

Muki Soomar

unread,
Feb 25, 1997, 3:00:00 AM2/25/97
to

I am looking for some examples of using the argc and argv
variables in a tcl script. Can someone please post some
so I can learn.

Is there something like a "main" in tcl script that
is the main script calling different procedures.

--
A train station is where a train stops
A bus station is where a bus stops
I have a workstation on my desk...

Jeffrey Hobbs

unread,
Feb 25, 1997, 3:00:00 AM2/25/97
to

In article <331329...@ford.com>, Muki Soomar <mso...@ford.com> wrote:
>I am looking for some examples of using the argc and argv
>variables in a tcl script. Can someone please post some
..

>Is there something like a "main" in tcl script that

argc and argv are special global variables that work just like the
C ones with one exception: the name of the script is not the first
element of argv (which, BTW, is a list). That is instead placed in
its own special variable argv0. When using these in a proc, make
sure to declare them global. There are endless examples of their
use in the books and any real Tcl/Tk app. If you have TkCon, you
could look for where argc/argv are declared global and see how I
deal with them.

There is no 'main' in a Tcl script, it just starts processing
when it comes to the first elements of a script (although 8.0
will actually preprocess).

--
Jeffrey Hobbs jho...@cs.uoregon.edu,@or.cadix.com
Software Engineer, Oregon R&D office: 541.683.7891
CADIX International, Inc. fax: 541.683.8325
URL: http://www.cs.uoregon.edu/~jhobbs/

Keith Vetter

unread,
Feb 25, 1997, 3:00:00 AM2/25/97
to

In article <331329...@ford.com>, Muki Soomar <mso...@ford.com> wrote:
>I am looking for some examples of using the argc and argv
>variables in a tcl script. Can someone please post some
>so I can learn.


Well, here's the routine I use to parse command switches which uses
argc and argv. This example lets the user specify two options: -q (for
quiet) and -f<filename> (for a filename). Also, '--' can be used to
terminate a list of switches, and any other switch causes an error.


proc parse_args {} {
global argc argv
global filename quiet ;# Variables for each switch

for {set a 0} {$a < $argc} {incr a} {
set arg [lindex $argv $a]
switch -regexp -- $arg {
^-o$ { set filename [lindex $argv [incr a]]}
^-o { set filename [string range $arg 2 end]}
^-q$ { set quiet 1}

^--$ { incr a; break }
^- { puts "unknown option $arg\n" ; usage }
default { break }
}
}
set argc [expr $argc - $a]
set argv [lrange $argv $a end]
}

set filename "default filename"
set quiet 0
parse_args ;# Read the command switches

puts "remaining arguments are: $argc: '$argv'"
return


>Is there something like a "main" in tcl script that

>is the main script calling different procedures.

By default, there is no main--tcl programs execute each line in order.
But, I find my programs are better organized if I do have a "main" type
procecdure. In that case, the last line of the script is a call to
"main".

Keith
--
Keith Vetter | He was born with the gift of laughter
UC Berkeley, Dept. of EECS | and a sense that the world was mad.
kei...@cs.berkeley.edu | Raphael Sabatini
http://www.cs.berkeley.edu/~keithv/ |

0 new messages