I usually roll my own, because its easier and much more specific to the program, but
I have some go to functions that help.
The tcl shell tclsh gives you several things from the command line:
$::argv0 # the name of the script being run
$::argc # the number of args
$::argv # a list of argument strings
[info script] # the value of the script name
# like "main"
# if true this is the script being run
if { [info script] eq $::argv0 } {
if { $argc eq 0 } {
puts stderr "no args given"
exit 1
}
set lastopt {}
set optarg 0
set verbosity 0
set inputfile {}
set outputfile {}
foreach arg $::argv {
if { $optarg } {
if {$lastop eq -o} {
set outputfile $arg
}
if {$lastop eq "-v" } {
set verbosity $arg
}
# ... more option args
set optarg 0 ; # reset
continue ; #
}
switch $arg {
- h {
help ; # print help
exit 0
}
-o {
set lastop -o
set optarg 1
}
default {
set inputfile $arg
set outputfile "output.txt"
}
} ; # switch
} ; # foreach argv
something like that that "consumes" args, and
does an extra step for options with arguments to
get the string.
Sometimes it is helpful to shift the argument list as you pick off
values.
# shift a list left
# return removed item
proc lshift listVar {
upvar 1 $listVar l
set r [lindex $l 0]
set l [lreplace $l [set l 0] 0]
return $r
}
or you can sort a list and take out stuff you want
For instance perhaps you want to have a whole list of filenames
after the options, so you remove the options from the list after processing them
# remove from list by value
proc lremove {listVariable value} {
upvar 1 $listVariable var
set idx [lsearch -exact $var $value]
set var [lreplace $var $idx $idx]
}
variable optlist {}
variable arglist {}
foreach arg $::argv {
if { [string match "-*" $arg] } {
lappend optlist $arg
} else {
lappend filelist $arg
}
Tcl is pretty good for this kind of text processing.