Command-Line Arguments

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 2.  Getting Started


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\TCL82\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 59, to extract items from it:

 set arg1 [lindex $argv 0] 

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

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 newUse a new private colormap. See page 540.
-display displayUse the specified X display. UNIX only.
-geometry geometryThe size and position of the window. See page 572.
-name nameSpecify the Tk application name. See page 562.
-syncRun X synchronously. UNIX only.
-use idUse the window specified by id for the main window. See page 580.
-visual visualSpecify the visual for the main window. See page 540.
--Terminate options to wish.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net