Using TCL cmdline library with Interactive Tools

Most tools in EDA (Electronic Design Automation) have an interactive TCL (Tool Command Language) interface.  Some examples are Mentor Questa, Cadence Incisive, Cadence RTL Compiler, and many of the IBM EDA tools.  A common problem is that the TCL language does not come shipped with a command line parser.  TCL assumes you will just use positional arguments or you will come up with your own parsing package.

The Standard TCL Library

The Standard TCL Library (TCLLIB) has many routine libraries; including a very useful command line parser.  Unfortunately, it is not always installed by default or included with EDA tools implementation of TCL.  For a good intro into the use of the command line parsing library available from TCLLIB, you should check out a post from Hai's Blog on how to use the TCLLIB cmdline library.

I took the the example from Hai's Blog and wrapped it in a reusable way for use within EDA tools.  Since EDA tools already have the TCL interpreter running, you would want to have the TCL arguments wrapped around procedures and not the arguments to program itself.

To see the source check it out the code cmdline_demo.tcl on my github.com repository.

Example Use

> tclsh

% source cmdline_demo.tcl
% ::cmdline_demo::ssh_example
-----
args = '-host lsim01 -port 32 -user cmdline_demo -verbose'

arg(host) = lsim01
arg(port) = 32
arg(user) = cmdline_demo
arg(verbose) = 1

-----
args = '-host lsim01 -port 23 -user ljohn'

arg(host) = lsim01
arg(port) = 23
arg(user) = ljohn
arg(verbose) = 0

-----
args = '-host lsim01 -user cmdline_demo -verbose'

arg(host) = lsim01
arg(port) = 22
arg(user) = cmdline_demo
arg(verbose) = 1

-----
args = '-host lsim01 -user ljohn'

arg(host) = lsim01
arg(port) = 22
arg(user) = ljohn
arg(verbose) = 0

The code above starts by starting the TCL shell tclsh and then sourcing the cmdline_demo.tcl file to load in the example code.  We then run the "::cmdline_demo::ssh_example" procedure to show how the library parses various command line arguments.  It takes the raw arguments to the command and then puts them into TCL array accessible by the key of "arg" here.  You can see that the cmdline library provides optional defaults to variables.

You can do the same with Mentor Questa below.

Showing Using Mentor Questa Run the Example

Showing Using Mentor Questa Run the Example

 

Advantages

One of the awesome advantages of using this the cmdline library is that it is self documenting for arguments it provides.

> ::cmdline_demo::ssh -?
-----
args = '-?'
tclsh cmdline_demo::ssh ?options?
-host value STRING: hostname to connect to <>
-port value INT: port to connect to (default 22) <22>
-user value STRING: username <>
-verbose FLAG: set the verbose ssh option on (default off)
-- Forcibly stop option processing
-help Print this message
-? Print this message

If you do "-help" or "-?" it will print out the possible arguments it is looking for and the defaults.

Short Falls

The cmdline library is a parser and not a verifier.  It won't check if you are sending in a value in the correct range, or type, or even if you do not provide a needed option.  You have to check those yourself, but parsing it out is a done for you in a consistent and simple way.

Another shortcoming is that you can't assume that TCLLIB will exist on the host even if TCL is installed.  The Redhat machines I'm using didn't have it installed by default.  Interestingly, Mentor Questa did have this library available from their own native TCL install even though it was not available from the Redhat installed system TCL.

The fix if your machine does not have TCLLIB installed, is to just include the TCL for the library you need with it.  I included a copy of the cmdline 1.3.3 here on github as an example.  Just put it with your TCL program and then source it.  TCLLIB is written in pure TCL so it doesn't need a lot of supporting files or compiled binaries; so the use of just using their cmdline library without using the rest actually works!

The example program I wrote on Github also shows this method of using the library directly.

Conclusion

The Standard TCL Library provides a package to parse arguments from the Linux command line as well as arguments to interactive procedures.  It provides a standard way to parse arguments and to gives optional defaults to those arguments.  Since TCL is so prevalent in EDA tools, it would be a good idea to have custom code have a familiar look and feel to them - and cmdline provides that.

References