Passing arguments in the command line in TCL

tcl
14,308

If you understand how the call to Tcl works, then you should know that in the following line, two arguments are given, namely first and "second arg" (The quotes are needed to make sure the 2nd argument consists of two words:

c:\> tclsh printargs.tcl first "second arg"

Then the following instruction:

foreach arg $::argv {puts $arg} 

...makes use of the control structure:

foreach <variable-name> <list> { <commands> }

So, arg is the variable name (reading this variable requires the addition of a $ sign -> $arg).

The $::argv is actually a global variable containing a list of the command line arguments (containing first and "second arg").

The puts $arg is the command to print the contents of the $arg variable to the screen.

But there are probably more simpler examples to start with. If you want to expand your skills, please read some Tcl/Tk tutorials or books. Brent Welch's Practical Programming with Tcl/Tk is a good start.

Share:
14,308
madhu131313
Author by

madhu131313

I code in whichever language is needed for the idea to be executed rightly!

Updated on July 15, 2022

Comments

  • madhu131313
    madhu131313 almost 2 years

    From the Tcl FAQ for Windows:

    To run a console script, invoke the tclsh.exe program, passing it the path to the script file. Any additional arguments on the command line are passed as a list to the script in the argv global variable

    # File printargs.tcl
    foreach arg $::argv {puts $arg} 
    

    We can invoke this script from the command line:

    c:\> tclsh printargs.tcl first "second arg"
    first
    second arg
    c:\>
    

    I am able to do this.
    But how is this working?
    How the arguments are going?

    note: I am a beginner.Sorry If I don't reach the standards.