Command line arguments
With any scripting language, the ability to provide arguments allows you to write a script that accepts arguments to perform a specific function.
As previously discussed, Tcl has several global variables to allow for the passing of command line arguments. The number of command line arguments to a Tcl script is passed as the global variable argc
. The name of a Tcl script is passed to the script as the global variable argv0
, and the arguments are passed as a list in the argv
global variable.
Launching a Tcl script
In the following example we will invoke a Tcl script contained within a text file. This script will accept any number of arguments and print out the script name, the count of the arguments and the values contained within the argv
variable.
Getting Ready
To complete the following example we will need to create a Tcl script file in your working directory. Open your text editor of choice and follow the instructions below.
How to do it…
Create a text file named args.tcl
that contains the following commands.
# If no command line arguments are passed perform no actions if {$argc > 0} { # Print out the filename of the script puts "The name of the script is: $argv0" # Print out the count of the arguments passed puts "Total count of arguments passed is: $argc" # Print out a list of the arguments puts "The arguments passed are: $argv" # Using the List Index of argv print a specific argument puts "The first argument passed was [lindex $argv 0]" }
After you have created the file invoke the script with the following command line:
% Tclsh85 args.Tcl ONE 2 3 The name of the script is: args.Tcl Total count of arguments passed is: 3 The arguments passed are: ONE 2 3 The first argument passed was ONE %
How it works…
As you can see, the script accepts any number of arguments and using the Tcl global variables allows access to the arguments passed as either a list or individual values. Keep in mind that when passing control characters, they must be escaped using the backslash character.
There's more…
Invoke the script with the following command line:
% Tclsh85 args.Tcl \home \etc The name of the script is: args.Tcl Total count of arguments passed is: 2 The arguments passed are: home etc The first argument passed was home %
In the above example you can see that the backslash characters are removed. This is NOT done by Tcl, but rather by the shell from which Tcl was invoked.
Now invoke the script with the escape character added:
% Tclsh85 args.Tcl \\home \\etc The name of the script is: args.Tcl Total count of arguments passed is: 2 The arguments passed are: {\home} {\etc} The first argument passed was \home %
By adding the escape character the backslash characters are retained and curly braces have been appended to define the values as strings. For UNC file paths that contain double backslash characters you would need to enter one escape character for each backslash for a total of four. You may also 'protect' the data by enclosing it within quotes, however this is a feature of the shell used to invoke Tcl and not the Tcl shell.