Browse available extensions and their commands

Purpose: to provide a command that allows one to see what extensions/packages are available within a particular interpreter's environment, as well as what commands/procs are available within a particular extension/package.


The following code is a first draft at providing this functionality. It is incomplete - currently the code only outputs a sorted list of packages available to package require, along with their versions.

I'll hopefully have time to enhance this to add the ability to list commands from a package - once I figure out how to do that.


LV Version 1.2 doesn't work right now - I have to figure out how to get $::i down into the interpreter. Then, in theory, I should see the list of unique commands.


 #! /usr/tcl84/bin/tclsh

 # Name: WhatsAvailable
 # Purpose: List the packages, versions, and commands within a 
 #              package able to be package required by the tcl interpreter
 # Version: 1.2
 # Author: Larry W. Virden

 set a [eval [package unknown] Tcl [package provide Tcl]]
 set pkglist [lsort -dictionary [package names]]
 foreach ::i $pkglist {
        puts $::i
        set a [lsort -dictionary [package versions $::i] ]
        puts [format "%s, versions: %s " $::i $a]
        flush stdout
        set b [interp create $::i]
        interp eval $b {
                # capture sorted prior command list
                foreach j [lsort -dictionary [info commands] ] {
                        set cmds($j) 1
                }
                puts $::i
                package require $::i
                # capture sorted current command list
                set new [list]
                foreach j [lsort -dictionary [info commands] ] {
                        if { $cmds($j) != 1 } {
                                set new [list $new $j]
                        }
                }
                puts $new
        }
        interp delete $b
 }

Basically listing commands provided by a package could be done by creating a slave interpreter, storing all existing namespaces and command names, loading the package in the slave, looking at new namespaces and commands by comparing with the stored state. Afterwards one can delete the slave interpreter. This should work memory neutral with script only packages, but could be a problem with c-coded extensions, that cannot be unloaded so cleanly. Michael Schlenker


See also:


Category Application - Category Introspection