Book Image

Tcl/Tk 8.5 Programming Cookbook

Book Image

Tcl/Tk 8.5 Programming Cookbook

Overview of this book

With Tcl/Tk, you can create full-featured cross-platform applications in a simple and easy-to-understand way without any expensive development package; the only tools required are a simple text editor and your imagination. This practical cookbook will help you to efficiently interact with editors, debuggers, and shell type interactive programs using Tcl/Tk 8. This cookbook will comprehensively guide you through practical implementation of Tcl/Tk 8.5 commands and tools. This book will take you through all the steps needed to become a productive programmer in Tcl/Tk 8. Right from guiding you through the basics to creating a stand-alone application, it provides complete explanation of all the steps along with handy tips and tricks. The book begins with an introduction to the Tcl shell, syntax, variables, and programming best practices in the language. It then explores procedures and the flow of events with control constructs followed by advanced error trapping and recovery. From Chapter 4, a detailed study of string expressions and handling enables you to handle various string functions and use lists to expand the string functionality. The book then discusses in-depth the Tcl Dictionary and how to utilize it to store and retrieve data. File operations and Tk GUI handling are covered extensively along with a developing a real-world address book application to practice the concepts learned.
Table of Contents (20 chapters)
Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface

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.