| [ Team LiB ] |
|
Command-Line ArgumentsIf 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 WishSome 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:
|
| [ Team LiB ] |
|