0

I am trying to run a tcl script through .bat file. I want to read some cmd arguments in the tcl script. Below is my code:

Command to run:

D:\Cadence\Sigrity2021.1\tools\bin\PowerSI.exe -tcl abcd.tcl %new_var%.spd %new_file_name%

Below is how I am trying to read the variable in the tcl file:

sigrity::open document [lindex $argv 0] {!}

It open up the Cadence Sigrity, but I see the below error:

screenshot of error

How do I read cmd argument in tcl?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • `argv` is a global variable. If you're using that sigrity command in a proc, use the qualified name: `$::argv` – glenn jackman May 09 '22 at 17:59
  • I am using it directly in the tcl file. Changed to `$::argv`. Seeing below error: TCL Result(Line 1): can't read "::argv": no such variable – user18994682 May 09 '22 at 18:21
  • If I look at the [tclvars](http://www.tcl-lang.org/man/tcl8.6/TclCmd/tclvars.htm#M46) documentation, I see "The following variables are only guaranteed to exist in `tclsh` and `wish` executables; the Tcl library does not define them itself but many Tcl environments do." -- you'll have to read the documentation for PowerSI to see how to access the command line arguments. – glenn jackman May 09 '22 at 18:25
  • I unfortunately couldn't fine anything. Do you know if its even possible with PowerSI? – user18994682 May 09 '22 at 19:35
  • No idea. I don't know anything about it. – glenn jackman May 09 '22 at 20:04
  • Could group the script name and the arguments together with the `-tcl` option like this? `-tcl "abcd.tcl %new_var%.spd %new_file_name%"` – Chris Heithoff May 09 '22 at 20:07
  • You might have to pass the arguments by writing a little file that sets them and calls your real script. Which would be gross, but workable. – Donal Fellows May 10 '22 at 13:53

1 Answers1

0

If you have no other way to do it that you can find (and it sounds like that might be the case) then you can fake it by writing a helper file with content like this, filling in the real arguments in the appropriate places:

# Name of script to call
set ::argv0 "abcd.tcl"
# Arguments to pass
set ::argv {}
lappend ::argv "%new_var%.spd"
lappend ::argv "%new_file_name%"
# Number of arguments (rarely used)
set ::argc [llength $::argv]
# Do the call
source $::argv0

Then you can pass that file to PowerSI and it will set things up and chain to the real file. It's messy, but practical.


If you're writing this from Tcl, use the list command to do the quoting of the strings (instead of putting them in double quotes) as it will do exactly the right thing for you. If you're writing the file from another language, you'll want to make sure you put backslashes in before \, ", $ and [ characters. The fiddlyness of doing that depends on your language.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thanks! Are you suggesting to use this script in the tcl file or bat file? I am calling the .tcl script using bat file. – user18994682 May 14 '22 at 04:54
  • You write the script any way you want; the bytes don't care where they came from. The point of it is to be a small *dramatically generated* Tcl script that sets up the variables and calls the real script. It is ugly, but it is all you can do when you don't have access to the real arguments. – Donal Fellows May 14 '22 at 20:25