Tcl $argc and $argv variables

Flaxter picture Flaxter · Jan 28, 2015 · Viewed 9.2k times · Source

I'm kinda new to tcl but I have to write a proc that looks like this:

proc TestVerb { Data Data_txt } {
VERBATIM [format "// Data: $Data - $Data_txt"]
if { $argc == 2} {
    VERBATIM {// SUCCESS //}
else {
    exit 1
}

I call the proc like this: TestVerb Switch"This is used for..."

The proc is in a different file and the proc call is in another one. They seem properly sourced because I get the desired output if I do not use $argc but once I use either $argv or $argc I get the following compilation error: Can't read $argv/$argc no such variable

If I refer to this variables with $::argc and $::argv the result is not correct. $argv is empty and $argc is 0

Answer

Donal Fellows picture Donal Fellows · Jan 28, 2015

The argc and argv variables are global variables (when they have any special meaning). Inside a procedure, they're not special at all unless you say:

global argc argv

or use the fully-qualified versions.

With your procedure, you will get an error message (generated by the procedure call machinery) unless you pass in the right number of arguments. You don't need to check yourself (unless you use formal parameters with defaults or the special args variable — note the spelling — at the end of the formal parameters.

To get the exact list of arguments passed in to your procedure, use:

set allArguments [info level 0]

You normally don't need it. You definitely don't need it when declaring your procedure like:

proc TestVerb { Data Data_txt } {
    ...
}