SrcFile

I had a file intended to be sourced as well as evalled - that is, it was an executable file but could also be sourced into a running tcl program. Problem: "source" does not take args. So it was impossible to use the same argument parsing system for both modes. Or is it?

  proc srcfile { filename args } {
    global argv

    set argv $args
    source $filename
  }

This proc will, of course, mung your argument vector, so make sure you have parsed it before "srcfile" is used.

NEM: Or you could save and restore it:

 proc srcfile-safe {filename args} {
     global argv

     set save $argv
     set argv $args
     set rc [catch {source $filename} ret]
     set argv $save
     return -code $rc $ret
 }

MGS [2010-05-14]: Note: the above code should really use uplevel to source the file into the calling namespace, and also set the argc variable for completeness.


See also: