When I get into tcl introspection I always feel uncomfortable because there's a plethora of methods to get things done (info, package, namespace, array...) and sometimes you need some king of scripting or it's not possible to get it at all .
This time I was wondering how I can discover the subcommands a command (proc) or ensemble provides. Of course there's no direct way. You have to do some scripting but the problem is it is not the same scripting.
Following this stackoverflow question [1] if you want to know what subcommands a command or proc provide you should guess by failing, you call the command with a "not used subcommand for sure" and interpret the message saying there no such subcommand and
providing the available commands. Something like:
proc listOptions args {
try {
{*}$args -!
} on error msg {
if {[regexp {should be one of (.*)} $msg -> items]} {
return [string map {{, or } { } , {}} $items]
}
}
error "no option list from $args"
}
puts [listOptions chan configure stdout]
The problem is this doesn't work as a general solution because parsing the output fail when output displays a subtle different error message for different things (subcommands, options...) so parsing should be adapted for every case:
% chan k
unknown or ambiguous subcommand "k": must be blocked, close, configure, copy, create, eof, event, flush, gets, names, pending, pipe, pop, postevent, push, puts, read, seek, tell, or truncate
% chan configure stdout k
bad option "k": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation
% array k
unknown or ambiguous subcommand "k": must be anymore, donesearch, exists, get, names, nextelement, set, size, startsearch, statistics, or unset
On the other hand ensembles also requires scripting but a different one, ensemble have a "discovering command" to list all subcommands available, so to list the subcommands of ensemble string you can do:
dict keys [namespace ensemble configure $cmd -map]
So this is a real mess, and there's no a unified way to do introspection
It would be great to have a common introspection command, for example info, to gather all kind of info relating variables, commands, namespaces, packages, etc
Or at least, wouldn't be great if every command have a subcommand just to list all subcommands available, and even every subcommand a subcommand to list the options accepted ?
Maybe something like:
% chan help
blocked, close, configure, copy, create, eof, event, flush, gets, names, pending, pipe, pop, postevent, push, puts, read, seek, tell, truncate
% chan configure stdout help
-blocking, -buffering, -buffersize, -encoding, -eofchar, -translation
% array help
anymore, donesearch, exists, get, names, nextelement, set, size, startsearch, statistics, unset
% string help
bytelength, cat, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, reverse, tolower, toupper, totitle, trim, trimleft, trimright, wordend, wordstart
regards
[1]
https://stackoverflow.com/questions/75076109/tcl-how-to-get-the-list-of-all-possible-sub-commands-switches-for-a-given-tcl
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)