Updated 2015-04-25 19:22:44 by pooryorick

Several Tcl commands, including expect and switch, take a '--'. RS calls this, "the switch to end all switches". Here's why:

What is the difference between
    switch $variable {...

and
    switch -- $variable {...

? None--mostly; if, however, $variable contains a string such as "-glob", then the first form will be interpreted as
    switch -glob {...

Do you see the problem? Do you see how your datum might be interpreted as a command "switch"? In casual English words, the '--' says, "treat everything that follows like data, not as a 'switch' or 'flag' ..."

Alas, not every tcl command which takes a switch/flag recognizes -- as a valid entry. This means that the casual tcl programmer needs to check regularly to see if the command being coded accepts the -- or not. It surely would be nice if all tcl commands which take a -argument also accepted -- ...

MG Not all commands need them. For instance, lsearch takes -args, but its last two arguments are always the list and the value to find, so any other arguments are always switches. It's only really in things like switch, regexp, etc, which can take both a variable number of -arg switches and a variable number of string arguments, where the -- switch is needed to tell where -args end off and 'normal' args begin. (Not that it would hurt if -- was available for the others too, but I think that's why it's not currently.)

AMG: This form of switch is an example of a command that doesn't need -- [1]. Since it (presumably) contains all match patterns and bodies grouped in a single word, switch knows that the second-to-last word is the match string and not an option.

Also, let's call them options and not switches, to avoid confusing them with the [switch] command. ;^)

So, what's a valid example of a place where -- is needed? The alternate form of [switch] is one:
switch $variable\
pattern1 {body1}\
pattern2 {body2}\
pattern3 {body3}

Another is [regexp]:
regexp -- {-[a-z]+} $argument

Although I omit the -- option to regexp if I can guarantee the regular expression does not begin with a minus sign.

Others? It would be nice to have a more-or-less complete list.

AMG: In general, -- is needed when a command takes any number of option arguments followed by any number of non-option arguments. When the number of non-option arguments is fixed (e.g. always one) or is a function of the option arguments, -- isn't needed to distinguish between options and non-options.