"command options" suggests several distinct topics:
- tclsh and wish arguments
- command-line options as seen (through $::argv) of Tcl-coded applications
- idioms for parsing variable (args) arguments seen by pure-Tcl procs.
Certain considerations are common to all these, and are convenient to treat here in a unified way.
- getopts [L1 ] [mildly buggy]
- clig (command line interpreter generator) [L2 ]
- Extral's args_parse, cmd_parse, and so on
- super getopts [L3 ]
- optcl
- Laurent Demailly's opt
- jstools includes an ... [?] package for argument parsing
- yaap
- GenParseCmdLine
- cmdline in tcllib. This is probably the most standard and widely-used of these packages.
- ::tcl::OptProc is shipped with Tcl, but deprecated.
- optparse is in 0.4 of tcllib. It's also deprecated, in favor of cmdline. [*Is* it in tcllib0.4? In any case, it's deprecated ...]
- argp (optional argument parser) parses commandline arguments and optional arguments of procs [L4 ]
- Doug Simpson posted [L5 ] his "groom" to comp.lang.tcl.
- There is argument processing code at http://www.MapFree.com/sbf/tcl/scripts1.html by jazimmer .
- Michael Kraus has code in http://ourworld.compuserve.com/homepages/mmg_kraus/mkGenMan.htm for processing arguments to procedures.
- The Simple Library [L6 ] includes both a package for command arguments handling including typed arguments (with optional tun-time checking) and options (SimpleProc, [L7 ]) and a very powerful command line options parser with too many features to list here (SimpleOptions, [L8 ]).
- SNTL at http://www.csua.berkeley.edu/%7Esls/woa/distrib/ contains code for command line argument processing as well as many other items. It was written by Sam Shen.
- YAAP is Yet Another Argument Parsing utility and can be found at ftp://ftp.neosoft.com/languages/tcl/sorted/packages-7.6/devel/yaap-0.9.shar.gz . It is a template based argument parsing utility inspired by XtGetApplicationResources() .
- evaluate_parameters at ftp://ftp.Lehigh.EDU/pub/evap/evap-2.x/evap-2.2.tar.Z is a Perl program that processes command line arguments in a simple consistent manner performing type-checking, multi-levels of help, etc. a Tcl/Tk GUI wrapper around one's Perl or C program to gather the command line arguments interactively.
[Many people write their own "... -arg1 val1 -arg2 val2 ..." processing, because it's so easy to use Tcl associative arrays (see "Arrays / Hash Maps") simply as the "optional arguments" section in Tcl Gems does ...]
Here is the start of some code to show at least one method of doing command line parsing. Hopefully people will contribute other samples as appropriate.
# If this script was executed, and not just "source"'d, handle argv
if { [string compare [info script] $argv0] == 0} {
while {[llength $argv] > 0 } {
set flag [lindex $argv 0]
switch -- $flag {
"-bool" {
set bool 1
set argv [lrange $argv 1 end]
}
"-option" {
set value [lindex $argv 1]
set argv [lrange $argv 2 end]
}
default { break }
}
}
}
foreach file $argv {
puts "[format "file: %s" $file]"
}
A quick and dirty way uses one-liners like this:
if [regexp " -x" $::argv] {# do the X thing} ;# RS
If your values come in pairs (like -option value), I usually use the following code for parsing them:
foreach {option value} $argument_list {
switch -glob -- $option {
-opt* {set opt $val}
-otheropt* {set otheropt $val}
default {error "Unkown option $option!"}
}
}
A related topic: "Syntax parsing in Tcl".
Tcl syntax help - Arts and crafts of Tcl-Tk programming