Updated 2015-08-17 23:54:38 by RLE

Richard Suchenwirth 2006-12-08 - Tcl scripts started from the command line can often be configured with switches (also called flags) like
 $ myscript.tcl -v -sep ';' whatever

Here's my little take to parse out switches from the command line (argv) by name. If a variable name is given, the word following the switch is assigned to it; in any case, the flag (and possibly the consumed following word) are removed from the command line, and 1 is returned if the flag was found, else 0.
 proc getopt {_argv name {_var ""} {default ""}} {
     upvar 1 $_argv argv $_var var
     set pos [lsearch -regexp $argv ^$name]
     if {$pos>=0} {
         set to $pos
         if {$_var ne ""} {
             set var [lindex $argv [incr to]]
         set argv [lreplace $argv $pos $to]
         return 1
     } else {
         if {[llength [info level 0]] == 5} {set var $default}
         return 0

Larry Smith see also init.

Usage examples:
    getopt argv -sep sep ";"      ;# possibly override default with user preference
    set verbose [getopt argv -v]  ;# boolean flag, no trailing word

Searching with -regexp allows to specify longer mnemonic names, so it still succeeds on longer flags, e.g.
 $ myscript.tcl -separator '\t' ...

If you use this separator for joining lists, make sure to subst -nocommand it before use.

wdb In my case, I use switches always with a value such that there is always a pair key - value which I collect into an array as follows:
 % proc testSwitch {arg1 args} {
        array set switch [concat {
                -accept no
        } $args]
        list arg1 was $arg1, -accept was $switch(-accept).
 % testSwitch Wurst
 arg1 was Wurst, -accept was no.
 % testSwitch Kaese -accept yes
 arg1 was Kaese, -accept was yes.

RS: true, named arguments can be handled simpler. What I wanted was an option extractor from command lines, which may contain different numbers of file names, etc.

escargo - Command line usage is sometimes complicated by options where the order is important and where some options can occur multiple times with different meanings. Some problematic cases:

  • -D, where each -Dname=value defines a new symbol definition
  • -V, where each occurrence of -V increases the level of verbosity of processing.
  • -X, where the final value associated with X determines the value used in all processing.
  • -Y, where the current value of Y is used for processing until a new value of Y is supplied.

People are far too clever about how options (or arguments) ought to be interpreted.


sbron: I created another reasonably small alternative getopt.

jrw32982 My take on basic getopts command line parsing.

yinjianhong 2015-08-17 Here is a getopt implementation in tcl that compat with GNU getopt_long_only(3)