Updated 2015-02-08 13:12:23 by dkf

Purpose: to provide guidelines, tips, techniques, etc. on writing Tcl/Tk/etc. scripts that wrap around an existing command line oriented application.

By wrap, I mean that the resulting script provides some additional ease of use or functionality over the original program, and that the user typically has minimal knowledge that the original application even exists.

Common examples are the creation of a GUI interface for a command-line application [1], and adding Tcl/Tk to a C application (as well as the cases which fit both those description).

Many people take advantage of Expect and Tk in writing GUI applications which interact with a character oriented application.

Expect is quite useful when you are wanting to interact with an application that is written to interact with users directly via the terminal. One neat example that the Expect book discusses is wrapping a command like ftp, for instance, and then providing your own hooks for adding new commands - say to gzip or ungzip files on your machine, or to define macros to perform common sets of ftp actions, etc. While this is indeed way cool, CL counsels that, in the twenty-first century, use of vfs or tcllib's ftp client library is a considerably more elegant, robust, portable, and generally maintainable alternative. LV offers, as an alternative view, that he would much prefer to use an existing ftp client and write a _small_ amount of add on code than to write an entire ftp client ;) ...

Tcl and Tk work just fine for writing application that take additional arguments or use the GUI to gather values to then be supplied to the wrapped command via its command line.

The logical place for most newcomers to start would be "How to Use C and Tcl Together" [2]; the inconvenience there is the larger one of reading back-issues of the C/C++ Users Journal.

[Lots more pointers: Fortran pages, wrap references, RE series, ...]

EKB Some tips for exec to wrap a command-line program (a Windows example, but should be pretty straightforward to adapt it...):

If you want to return control immediately to the calling program, use '&'

For example,
 tk_messageBox -message "Before loading"
 exec Notepad.exe
 tk_messageBox -message "After loading"

will pop up "After loading" only after you close Notepad, while
 tk_messageBox -message "Before loading"
 exec Notepad.exe &
 tk_messageBox -message "After loading"

will pop up "After loading" immediately after opening Notepad.

Always wrap in a catch command - this will capture anything sent to stderr by the program
 if [catch {exec MyProg.exe my command line} errors] {
   # Clean up here...

Make sure you're in the proper directory for running the program, or provide a full path
 set currdir [pwd]
 cd "C:/some dir/to run/program/in"
 if [catch {exec MyProg.exe my command line} errors] {
   # Clean up here...
 cd $currdir

[More write-ups will appear in March 2006.]

escargo 8 Mar 2006 - I note that there may be two distinct issues here.

  1. How do I wrap Tcl/Tk around a program that gets all of its parameters from its invoking command line.
  2. How do I wrap Tcl/Tk around a program that is started from a command line (perhaps with parameters) and then interacts with a user through interactive commands and responses.

There are different techniques that could be applied to the two different cases.

There are also a couple of other interesting cases (orthogonal to the first pair).

  1. Is the program local to the system where I have access?
  2. Is the program remote from the system where I have access?

And another pair of questions:

  1. Is invoking the program the only step that I need to perform?
  2. Is invoking the program part of a larger command sequence that I need to perform?

"Scripted wrappers for legacy applications", Part 1 [3], 2 [4], and 3 [5], is a series from 2000-2001 which goes into a bit more depth on these questions.