Rahul Kumar
Rahul Kumar

Reputation: 11

TCL Error: wrong # args: should be "source ?-encoding name? fileName"

I am new to tcl

I wrote a code as ...

#!/usr/bin/tclsh


# get all co-ordinates of cell from arguments
# if suffiecient arguments are not there then print error and exit
# Calculate area of cell


lassign $argv a b c d e f g h
if {$argc != 8} {
puts "Error in argument count"
exit
} elseif {$b > $d} {
puts "Error: x1 is greater than x2 value"
exit
} elseif {$f > $h} {
puts "Error: y1 value is greater than y2"
exit
}

puts "Everything is correct proceed"

set area [expr ($d - $b) * ($h - $f)]
puts "Area of cell is $area"

when executing in mac terminal as...

source ./special_var.txt x1 20 x2 30 y1 30 y2 60

i am getting error as ...

wrong # args: should be "source ?-encoding name? fileName"

Upvotes: 1

Views: 1404

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

You've written a file that's trying to be a program (it has a #! line and depends on the argv global variable). That's fine, but you should run it with tclsh (explicitly or implicitly); you can't do it simply with source, which is a more primitive operation.

Running it as a subprocess is pretty easy:

exec [info nameofexecutable] ./special_var.txt x1 20 x2 30 y1 30 y2 60
# Or:
#   exec /usr/bin/tclsh ./special_var.txt x1 20 x2 30 y1 30 y2 60
# You might be able to guess what [info nameofexecutable] does...

or, if you have marked the file as executable in the OS:

exec ./special_var.txt x1 20 x2 30 y1 30 y2 60

Running in the current process is slightly trickier. The source command is more like #include in C; it doesn't do much special processing of arguments by itself, and if you want that then you have to do that first. It's not a lot of code...

set argv [list x1 20 x2 30 y1 30 y2 60]
source ./special_var.txt

We've never added that to source (and aren't going to!) because we don't usually want to interfere with argv just because we pull code from another file.

Upvotes: 1

Related Questions