Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Tcl/Tk 8.5 Programming Cookbook

You're reading from   Tcl/Tk 8.5 Programming Cookbook With over 100 recipes, this Cookbook is ideal for both beginners and advanced Tcl/Tk programmers. From the basics to creating applications, it‚Äôs full of indispensable tips and tricks to make the most of the language.

Arrow left icon
Product type Paperback
Published in Feb 2011
Publisher Packt
ISBN-13 9781849512985
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (20) Chapters Close

Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
1. Acknowledgment
About the Reviewers
2. www.PacktPub.com
3. Preface
1. The Tcl Shell FREE CHAPTER 2. Using the Building Blocks Control Constructs 3. Error Handling 4. Handling String Expressions 5. Expanding String Functionality Using Lists 6. The Tcl Dictionary 7. File Operations 8. Tk GUI Programming with Tcl/Tk 9. Configuring and Controlling Tk Widgets 10. Geometry Management 11. Using Tcl Built-in Dialog Windows 12. Creating and Managing Menus 13. Creating the Address Book Application

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.

You have been reading a chapter from
Tcl/Tk 8.5 Programming Cookbook
Published in: Feb 2011
Publisher: Packt
ISBN-13: 9781849512985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image