[ Team LiB ] Previous Section Next Section

Command-Line Arguments

If you run a script from the command line, for example from a UNIX shell, you can pass the script command-line arguments. You can also specify these arguments in the shortcut command in Windows. For example, under UNIX you can type this at a shell:

% myscript.tcl arg1 arg2 arg3

In Windows, you can have a shortcut that runs wish on your script and also passes additional arguments:

"c:\Program Files\Tcl84\wish.exe" c:\your\script.tcl arg1

The Tcl shells pass the command-line arguments to the script as the value of the argv variable. The number of command-line arguments is given by the argc variable. The name of the program, or script, is not part of argv nor is it counted by argc. Instead, it is put into the argv0 variable. Table 2-2 lists all the predefined variables in the Tcl shells. argv is a list, so you can use the lindex command, which is described on page 63, to extract items from it:

set arg1 [lindex $argv 0]

The following script prints its arguments (foreach is described on page 79):

Example 2-4 The EchoArgs script
# Tcl script to echo command line arguments
puts "Program: $argv0"
puts "Number of arguments: $argc"
set i 0
foreach arg $argv {
   puts "Arg $i: $arg"
   incr i
}

Command-Line Options to Wish

Some command-line options are interpreted by wish, and they do not appear in the argv variable. The general form of the wish command line is:

wish ?options? ?script? ?arg1 arg2?

If no script is specified, then wish just enters an interactive command loop. Table 2-1 lists the options that wish supports:

Table 2-1. Wish command line options

-colormap new

Use a new private colormap. See page 624.

-display display

Use the specified X display. UNIX only.

-geometry geometry

The size and position of the window. See page 658.

-name name

Specify the Tk application name. See page 648.

-sync

Run X synchronously. UNIX only.

-use id

Use the window specified by id for the main window. See page 667.

-visual visual

Specify the visual for the main window. See page 624.

--

Terminate options to wish.

    [ Team LiB ] Previous Section Next Section