Home > other >  TCL processing arguments template
TCL processing arguments template

Time:12-08

Following is a template from tcl wiki link, this code can be used to process tcl command-line arguments. I wonder why we need [set argv {}] at line 7.

# If this script was executed, and not just "source"'d, handle argv
if {[info exists argv0] && [
    file dirname [file normalize [info script]/...]] eq [
    file dirname [file normalize $argv0/...]]} {

    while {[llength $argv]} { 
        puts [list ooo $argv]
        set argv [lassign $argv[set argv {}] flag] ; # Notice Here

        switch -glob $flag {
            -bool {
                set bool 1
            }
            -option {
                set argv [lassign $argv[set argv {}] value]
            }
            -- break
            -* {
                return -code error [list {unknown option} $flag]
            }
            default {
                set argv [list $flag {*}$argv]
                break
            }
        }
    }
}

foreach file $argv {
   puts [format "file: %s" $file]
}

CodePudding user response:

This looks like the author implemented the performance optimization described in the "Unsharing Objects" section on the Tcl wiki page for K. Basically it makes the object unshared, so modification doesn't require a copy to be created. That can make a big difference when manipulating big lists in a loop. In this situation I don't expect it to provide much of a gain because argv will not normally be very big and it is probably only processed once.

  • Related