0

We implemented some tcl commands, if we type only part of the command at Tcl> prompt in tcl shell interactive mode, the tcl shell can recognize the command. But if we put the same partial command in tcl script file, then source the script from tcl shell, unknown command will be reported. And the whole command in tcl sript can be recognized.

How can we make source tcl script behavior same as shell interactive mode? We expect when source tcl script, partial command can also be reconginzed.

Please see following sample.

  1. Partial command can be recognized in shell interactive mode.
Tcl> my_prove
Info: proving started
Info: ....
Info: ....

Tcl> my_pro
Info: proving started
Info: ....
Info: ....
  1. Partial command cannot be recognized when source run.tcl script.

a) run.tcl

   setup_design
   my_prove
   my_pro

b) source the script run.tcl

Tcl> source run.tcl
$inside source$$> setup_desgin
    Design setup...
    Done
$inside source$$> my_prove
    Info: proving started
    Info: ....
    Info: ....
$inside source$$> ::unknown my_pro
    invalid command name "my_pro"
2
  • 2
    IMO, you should not do this, script files should contain the full command/option names. But, in an interactive tclsh session, examine the unknown proc (with info body unknown), and copy that implementation (or the relevant parts of it) into your tcl scripts/libraries. Commented Oct 23, 2020 at 14:04
  • Thanks, so it is recommended to use full command name in script file. It's ok. Commented Oct 27, 2020 at 1:57

1 Answer 1

2

Tcl implements partial command name expansion in the unknown procedure (along with numerous other things) and only turns it on in interactive mode. It's generally strongly recommended to use full command names in your scripts; that's far more likely to be reliable.

If you want to enable it, which I don't think you should, do this (this version uses tailcall so needs at least Tcl 8.6):

rename unknown _standard_unknown
proc unknown args {
    set cmd [lindex $args 0]
    set matches [uplevel 1 [list info commands $cmd*]]
    # If there is a unique match, use it
    if {[llength $matches] == 1} {
        lset args 0 [lindex $matches 0]
        tailcall {*}$args
    }
    tailcall _standard_unknown {*}$args
}

In theory, you can also set the tcl_interactive global to any true value (e.g., 1) to enable the expansion. However, there may be other unwanted behaviours as well enabled by doing that; Tcl makes no guarantees on that front.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Donal! Then I think use full name in script is ok if it will be more reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.