$ myscript.tcl -v -sep ';' whateverHere'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 wordSearching 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.
- care must be taken with multi-word-args like "the is not a -switch" to prevent from accidently finding the -switch inside the quoted string...
- See Matthias Hoffmann - Tcl-Code-Snippets - misc routines - command line parsing for a bigger but still light weight solution based on array set like above....
https://github.com/tcler/getopt.tclyinjianhong 2017-04-19 getopt.tcl 3.0-alpha release
https://github.com/tcler/getopt.tcl/blob/master/getOpt-3.0/example.tclRKzN at 2017-10-14 notes that tcllib's cmdline package includes a getopt command