简体   繁体   中英

Tcl - How to get the list of all possible sub-commands/switches for a given tcl command (including user defined commands)

For a given tcl user-defined command (for instance, "user_command -switch_A -switch_B") I would like to get all the possible sub-commands/switches to be used with that command as a list. Below is an example of the tcl regexp switches available: Regexp valid switches

I would like to use it to compare "implemented and public sub-commands/switches VS documented and public sub-commands/switches" for a given program.

I can get the available commands from "info commands" command, but I'm struggling to get possible valid sub-commands/switches for a given command.

The normal way is to use a deliberately wrong option name (such as -! ) and to catch and parse the result.

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]

To find subcommands for commands that are implemented as namespace ensemble s, you can do:

set cmd "string"
set map [namespace ensemble configure $cmd -map]
dict keys $map
# => bytelength cat compare equal first index is last length map match range repeat replace reverse tolower toupper totitle trim trimleft trimright wordend wordstart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM