• how to discover proc and ensemble subcommands AKA a better tcl introspe

    From pd@21:1/5 to All on Thu Jul 27 01:09:10 2023
    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)
  • From Arjen Markus@21:1/5 to All on Tue Aug 8 23:55:03 2023
    On Thursday, July 27, 2023 at 10:09:13 AM UTC+2, pd wrote:
    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 .

    ...

    Probably not quite what you are looking for, but the Nextcl project (https://wiki.tcl-lang.org/page/The+NexTcl+Project) seems useful in that respect. Stephan mentioned that he wants to work on a language server for Tcl. (See also his posts about the
    project)

    Regards,

    Arjen

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to All on Fri Aug 11 18:59:00 2023
    On 7/27/2023 1:09 AM, pd wrote:
    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


    You might find this website, by Ashok useful.

    https://www.magicsplat.com/tcl-docs/docindex.html

    This is especially handy if you can launch the url with a parameter using ?search= as shown on that page. If the parameter is unique, it will load the appropriate page, otherwise it will provide clickable choices. Try this one:

    https://www.magicsplat.com/tcl-docs/docindex.html?search=chan


    The first choice, chan tcl, gives you the full man page.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)