"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 [proc]s. Certain considerations are common to all these, and are convenient to treat here in a unified way. ---- * getopts [ftp://ftp.neosoft.com/languages/tcl/alcatel/code/tclGetOpts.tar.gz] [[mildly buggy]] * clig (command line interpreter generator) [http://wsd.iitb.fhg.de/~kir/clighome/] * [Extral]'s args_parse, cmd_parse, and so on * super getopts [ftp://ftp.Lehigh.EDU/pub/evap/evap-2.x/evap-2.2.tar.gz] * 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 [http://www.chevreux.org/projects_tcl.html] * ''ad_proc'' [http://www.arsdigita.com/api-doc/proc-view?proc=ad%5fproc&source_p=1] from the ACS [http://www.openacs.org/] * Doug Simpson posted [http://groups.google.com/groups?hl=en&safe=off&frame=right&th=b763b6651697f176] 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] [http://simplelib.bobsville.com] includes both a package for command arguments handling including typed arguments (with optional tun-time checking) and options (SimpleProc, [http://simplelib.bobsville.com/packages/SimpleProc.txt]) and a very powerful command line options parser with too many features to list here (SimpleOptions, [http://simplelib.bobsville.com/packages/SimpleOptions.txt]). * 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. ---- Question: In C, for Tcl, using the newer objc, objv API, how do I parse options? I would have thought people would need to do this all the time, but I can't find anything. Needs to handle args, options, required optional etc. Speed is high on the priority list... If you can use the Tk library, try Tk_SetOptions and its assorted support functions. It would be nice if something like this were in the Tcl library, for those times when one doesn't have Tk around. A pure-Tcl solution might be to do the option handling in Tcl, passing a fixed number of arguments to the C code, but that might not meet your speed requirements. ---- [[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]