How to handle command line arguments in TCL - tcl

I am writing a TCL scripts which expects command line arguments. Say the name of my script is myTcl.tcl , and in this case , invoking script with the command line arguments will look something like :
./myTcl.tcl -optA optA_arg1 optA_arg2 -optB -optC
How can I handle these in TCL ?(Is there any TCL equivalent of getopts from bash and if not , then how can it be done)
Thanks

There are several getopt-like implementations available. I prefer the one I wrote. There's also the cmdline command in tcllib and other options you can find on the Tcl wiki.

Related

Run Octave function with one argument from batch

I am trying to run an octave function from a batch file. The function is well written given how it works when launched from within the Octave GUI.
The batch file, other than pointing to the octave function, defines the only argument needed by it.
A while back this was not a function but a simple Octave script and the commands used were ok.
The only issue I am encountering now is being able to pass the variable calculated by the batch file onto the octave function.
I have recently written an octave function to do some file management. It requires only one input from the user:
function replace_TMM (file_base)
where file_base is a string to specify what directory I am working on. So it has to be something like "Z:" or "I:" or so on.
I am quite sure that the function is well written since I am able to use it from Octave GUI without any issues.
The fact is that I would like to run this function from a batch file. Inside this batch file I wrote:
SET a=%cd:~0,2%
This command is able to identify the working directory so "a" will be equal to "Z:" or similars.
Now my issue is telling the batch file to evaluate the octave function using "a" as its input argument.
I tried stuff like:
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval _03_REPLACE_V04("'%cd:~0,2%'")
which does not seem to work. This kind of solution gives a syntax error at batch level, it is not even able to enter the octave file.
If I instead try something like:
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q _03_REPLACE_V04.m Z:
It is able to enter the octave file but it does not process the function, just skips over it to get to the end of the script.
Same goes if I try the following:
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" _03_REPLACE_V04.m -"Z:"
In brief I bvelieve that the function itself works, it is only a matter of passing a variable from the batch to the octave.
Would really appreciate some help, thanks in advance.
UPDATE 1
I have done what was suggested by #Dariush Gavari and used the following syntax:
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval "replace_TMM('%a%')"
This gets me the following error message:
error: 'replace_TMM' undefined near line 1, column 1
I believed that it was because it was not ablòe to find the script containing the function. This is saved in a file called _03_REPLACE_V04.m
For this reason I have tried with
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval _03_REPLACE_V04.m "replace_TMM('%a%')"
Leading to the following error:
error: --eval "CODE" and script file are mutually exclusive options
usage: octave [-HVWdfhiqvx] [--debug] [--doc-cache-file file] [--echo-commands]
[--eval CODE] [--exec-path path] [--experimental-terminal-widget]
[--gui] [--help] [--image-path path] [--info-file file]
[--info-program prog] [--interactive] [--line-editing] [--no-gui]
[--no-history] [--no-init-file] [--no-init-path] [--no-line-editing]
[--no-site-file] [--no-window-system] [--norc] [-p path]
[--path path] [--persist] [--server] [--silent] [--traditional]
[--verbose] [--version] [file]
I believed that the problem could also have been having the functional nd the file with two different names. To solve this I have kept the same file name but changed the function to match it:
function _03_REPLACE_V04 (file_base)
Then in the batch:
"C:\Program Files\GNU Octave\Octave-7.3.0\mingw64\bin\octave-cli.exe" -q --eval "_03_REPLACE_V04('%a%')"
Leading to:
warning: function '_03_REPLACE_V04' defined within script file '\99_TOOLS\OCTAVE_FILES\_03_REPLACE_V04.m'
error: invalid call to script \99_TOOLS\OCTAVE_FILES\_03_REPLACE_V04.m
error: called from
_03_REPLACE_V04
In other words still no way of making it work. :)
Octave provides the argv function, which returns a cellstring array of all arguments passed to the octave executable at the time of launch, or in the case where it was used to launch a script, then this is the script's arguments.
So presumably all you have to do to get your directory from within octave is argv(){1}
If you would like to convert your filename to an absolute filename, you could also do this from within octave via the make_absolute_filename function.
Incidentally, a very useful command you should know of in octave is the lookfor command. Writing lookfor arguments in the terminal returns a list with all functions which have the word "arguments" in their description; argv is at the top of that list.
You can then do help argv to see more details on that command.

Converting Tcl to C++

I am trying to convert some tcl script into a C++ program. I don't have much experience with tcl and am hoping someone could explain what some of the following things are actually doing in the tcl script:
1) set rtn [true_test_sfm $run_dir]
2) cd [glob $run_dir]
3) set pwd [pwd]
Is the first one just checking if true_test_sfm directory exists in run_dir?
Also, I am programming on a windows machine. Would the system function be the equivalent to exec statements in tcl? And if so how would I print the result of the system function call to stdout?
In Tcl, square brackets indicate "evaluate the code between the square brackets". The result of that evaluation is substituted for the entire square-bracketed expression. So, the first line invokes the function true_test_sfm with a single argument $run_dir; the result of that function call is then assigned to the variable rtn. Unfortunately, true_test_sfm is not a built-in Tcl function, which means it's user-defined, which means there's no way we can tell you what the effect of that function call will be based on the information you've provided here.
glob is a built-in Tcl function which takes a file pattern as an argument and then lists files that match that pattern. For example, if a directory contains files "foo", "bar" and "baz", glob b* would return a list of two files, "bar" and "baz". Therefore the second line is looking for any files that match the pattern given by $run_dir, then using the cd command (another Tcl built-in) to change to the directory found by glob. Probably $run_dir is not actually a file pattern, but an explicit file name (ie, no globbing characters like * or ? in the string), otherwise this code may break unexpectedly. On Windows, some combination of FindFirstFile/FindNextFile in C++ could be used as a substitute for glob in Tcl, and SetCurrentDirectory could substitute for cd.
pwd is another built-in Tcl function which returns the process current working directory as an absolute path. So the last line is querying the current working directory and saving the result in a variable named pwd. Here you could use GetCurrentDirectory as a substitute for pwd.

"info command rtExMath" in Tk with vtk

I am new for Tcl/Tk. I am using vtk with Tk command window for running vtk tcl/tk examples. Here is a code which include Tk expression as condition of if and I am not getting it.
if { [info command rtExMath] != "" } {
##Do something related VTK
}
I have explored info of Tk but there is a combination with keyword command and also no any good explanation I found for rtExMath.
Please explain me above.
The info commands command (info command is just an unambiguous prefix) returns a list of all commands, or a list of all commands that match the given glob pattern. In the case you're looking at, the glob pattern is actually going to be a string-equality check (and is even optimised to such internally); there's no glob metacharacters in it. The result of that is that [info command rtExMath] != "" is a condition that is true exactly when the command rtExMath exists.
Tcl itself does not define any command called rtExMath; I conclude that it must be part of some specialist extension or application. However, Googling makes me suspect that it is actually a somewhat-standard name for an instance of the vtkMath class in Vtk, but I don't really know for sure. (I'm guessing that the binding of that class to Tcl was done by SWIG…)

octave history command - variable as filename

i want to write little helper functions that stores and loads the octave session.
function restoreSession(filename)
history -r strcat('./states/',filename,'.history');
load("-binary", strcat('./states/',filename,'.data'))
endfunction
function saveSession(filename)
history -w strcat('./states/',filename,'.history');
save("-binary", strcat('./states/',filename,'.data'))
endfunction
The save/load command works well.
My Problem is that the history command seems not to evaulate the argument.
it prodces the following error:
syntax error
>>> history -r strcat('./states/',filename,'.history');
^
I already tried to use a temporary var for the path but in this case it only interprets the variable name as filename and complains about the missing file.
Does anybody has an idea how to solve this?
Use history with the function syntax instead of a command.
history ("-r", strcat ("./states/", filename, ".history"));
All commands are actually functions. The command syntax (when you don't use parentheses) is available to all functions, it just happens that for some it looks more natural. When you omit the parentheses, all the arguments are interpreted as strings, even variable names. If you want to do something fancier, call them as functions.

TCL calling proc that is in another file, but the proc is not found: invalid command

I am new to TCL. I excute TCL code via: "tclsh85 FOD/Scripts/program1.tcl" from DOS window under Windows Vista.
program1.tcl included a "set junk [tproc $a]" and this give "invalid command name tproc".
tproc is defined in program2.tcl which is in directory "c:a/b/lib". What do I need to do to have TCL find the proc when I run "tclsh85 FOD/Scripts/program1.tcl"?
Did you source program2.tcl in program1.tcl ?
If not then you need to source "program2.tcl" before calling tproc
See http://tcl.tk/man/tcl8.5/TclCmd/source.htm
You should also familiar yourself with the Tcl package command at http://wiki.tcl.tk/9859