Posted to tcl by jima at Thu May 25 16:37:29 GMT 2017view pretty

Consider the following attempt to simulate a
subset of lsort:

   proc mylsort {{ordering -default ascii -switch {ascii dictionary
integer real}} list} {
        puts "Sort mode $ordering"
        lsort -$ordering $list
   }

It works with usual input:

   (test) 117 % set list {value some other thing}
   value some other thing
   (test) 118 % mylsort -dictionary $list
   Sort mode dictionary
   other some thing value

but fails when a dash is present [1]:

   (test) 119 % set list {-value some other thing}
   -value some other thing
   (test) 120 % mylsort -dictionary $list
   wrong # args: should be "mylsort
?|-ascii|-dictionary|-integer|-real|? list"

whereas the original lsort has no problem:

   (test) 121 % lsort -dictionary $list
   -value other some thing

It even breaks EIAS [1]:

   (test) 122 % lindex $list 0
   -value
   (test) 123 % mylsort -dictionary $list
   Sort mode dictionary
   -value other some thing

The remedy to this as per TIP 457 is the -- switch:

   (test) 124 % mylsort -dictionary -- $list
   Sort mode dictionary
   -value other some thing

This always works, but it puts the burden on the programmer to add -- at
all invocations. The current core "lsort" shows that the arguments are
never ambiguous, the assignment can be resolved by counting. The last
arg is the list, everything before is treated as an option. This
strategy always works if a fixed number of positional arguments is at
the beginning and/or end of the argument list. If core "lsort" were to
be implemented in TIP457, then ALL Tcl programs using lsort without the
-- switch would become buggy with a data dependent bug.