Command syntax definitions


Can they be automatically determined, I mean for existing commands, or derived using some standard tcl functions?

I'm sure the subject falls under the category of Introspection, where the bottom line seems to be that the answer is no.

I'm not looking for traversing widget hierarchies or complicated analysis, just a list of functions, and especially the command syntax in some form, preferably as powerfull as a formal syntax definition, and readable.

One can extract the functions arguments from a user defined function, and their defaults, and of course the body, but tcl functions seem to be be able to be tested for syntax and expansion from shortcuts, but not for simply which arguments they allow.

DGP Just a few notes on terminology. You refer to functions and user defined functions. In the Tcl world, we normally speak of commands and procs instead. Sometimes we even use the whole word and speak of procedures, but when we speak of functions, we usually mean something like sin() that belongs inside an expr.

There are some introspection commands that operate only on procs -- info args and info default -- that give you information about a proc's syntax expectations.

There is no corresponding introspection for a general Tcl command. It is conventional, however, for error messages to include such a summary. For a simple example:

 % set
 wrong # args: should be "set varName ?newValue?"

And of course you can always consult the DESCRIPTION section of the commands documentation.

KPV To reiterate a point made by DGP above: error messages are often invaluable to quickly learn the correct syntax, especially for figuring out the available options to a given command or widget. But you often have to give it the right kind of error to get the informative error message back:

 % string
 wrong # args: should be "string option arg ?arg ...?"
 % string xxx
 bad option "xxx": must be bytelength, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart
 % canvas .c
 % .c
 wrong # args: should be ".c option ?arg arg ...?"
 % .c asdf
 bad option "asdf": must be addtag, bbox, bind, canvasx, canvasy, cget, configure, coords, create, dchars, delete, dtag, find, focus, gettags, icursor, index, insert, itemcget, itemconfigure, lower, move, postscript, raise, scale, scan, select, type, xview, or yview