info commands

info commands ?pattern?

If pattern isn't specified, returns a list of names of all the Tcl commands in the current namespace, including both the built-in commands written in C and the procedures defined using proc. If pattern is specified, only those names matching pattern are returned. A match is determined according to the rules for string match. pattern can be a qualified name like Foo::print*. That is, it may specify a particular namespace using a sequence of namespace names delimited by ::, and may have pattern-matching special characters at the end to specify a subset of commands in that namespace. If pattern is a qualified name, the resulting list of command names has each one qualified with the name of the specified namespace.


MGS 2004-03-14: Don't forget that info commands will not tell you the names of all possible commands - some commands are auto-loaded using the unknown and auto_load mechanism. To get a list of all possible commands, I use something like this:

proc commands {{pattern *}} {
    return [lsort -unique [concat [
        info commands $pattern] [
        array names ::auto_index $pattern]
    ]]
}

# example
set tk_commands [commands ::tk::*]

Note: this will extract commands from the auto_index array which match the specified pattern and commands from all descendant namespaces (this may not be what you want or expect). See array names for more info.

You could try this version instead:

proc commands {{pattern *}} {
    if {![string match ::* $pattern]} {
        regsub -- :::: [uplevel namespace current]::$pattern :: pattern
    }
    set auto [array names ::auto_index $pattern]
    set list [info commands $pattern]
    foreach c $auto {
        if  {![string match ${pattern}::* $c]} {lappend list $c}
    }
    return [lsort -unique $list]
}

# example
set tk_commands [commands1 ::tk::*]
puts "Found \[[llength $tk_commands]\] commands:\n[join $tk_commands \n]"

This version will return a list of all fully-qualified commands in the calling namespace.

In namespace with partly qualified commands

HaO 2013-08-06: Don't know why, but to check if a ttk widget command exists (like ttk::spinbox which was introduced later), one must use ::ttk::spinbox to get a result with 'info commands':

% namespace eval gui {info command ttk::spinbox}

% namespace eval gui {info command ::ttk::spinbox}
::ttk::spinbox

% namespace eval gui {ttk::spinbox .s}
.s