home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 11
/
BUGCD1998_02.ISO
/
email
/
sime
/
simdemo.z
/
optargs.tcl
< prev
next >
Wrap
Text File
|
1997-12-09
|
3KB
|
104 lines
# optargs third cut
# -----------------
#
# changed name to optargs since it isn't related to parseargs
#
# made it work without extended Tcl for wider usability
#
# optargs second cut
# ------------------
#
# 'defargs' now available for defining optargs arrays instead of just 'set'
#
# defargs now does some sanity checking
#
# integer fields are checked for integerness
#
#
# optargs first cut
# -----------------
#
# parses an argument list of "-option" fields.
#
# Field types are text, integer and boolean. Only boolean does anything
# special -- booleans don't have to have an argument and set the variable
# to the opposite of the default value if the matching -option is found.
#
# For text and integer, if a default value is there it is set if the option
# isn't explicitly defined, else nothing.
#
# demo appears below the optargs proc
#
#
proc optargs {arguments descriptorArrayName} {
upvar #0 $descriptorArrayName argInfo
# while there are any arguments, make sure they're in the array,
# process according to rules there
while {$arguments != ""} {
set fieldId [lindex $arguments 0]
if {[string index $fieldId 0] != "-"} {
error "option '$fieldId' didn't start with dash"
}
set fieldId [string range $fieldId 1 end]
if ![info exists argInfo($fieldId)] {
error "tried to set argument '$fieldId' which did not exist."
}
set fieldDef $argInfo($fieldId)
set type [lindex $fieldDef 0]
set default [lindex $fieldDef 1]
if {$type == "boolean"} {
uplevel "set $fieldId [expr !$default]"
set arguments [lrange $arguments 1 end]
} else {
if {[llength $arguments] == 1} {
error "missing argument to option '$fieldId'"
}
set value [lindex $arguments 1]
if {$type == "integer"} {
if {![regexp {[0-9]*} $value]} {
error "'$fieldId' integer field isn't integer - '$value'"
}
}
uplevel "set $fieldId [list $value]"
set arguments [lrange $arguments 2 end]
}
}
# if any elements in the descriptor array have defaults and they
# don't already exist with a user-set value, set them up
foreach element [array names argInfo] {
set fieldDef $argInfo($element)
if {[llength $fieldDef] == 1} continue
if ![uplevel "info exists $element"] {
uplevel "set $element [list [lindex $fieldDef 1]]"
}
}
}
proc defargs {optArgsArrayName definitionList} {
upvar #0 $optArgsArrayName parseArray
foreach optionInfo $definitionList {
# -option type [option]
set len [llength $optionInfo]
if {($len < 2) || ($len > 3)} {error "bad # elements in '$optionInfo'"}
set option [lindex $optionInfo 0]
set type [lindex $optionInfo 1]
if {($type == "boolean") && ($len < 3)} {
error "boolean option defs must have a default value ('$optionInfo')"
}
if {[string index $option 0] != "-"} {
error "option text in option def '$optionInfo' doesn't start with -"
}
set parseArray([string range $option 1 end]) [lrange $optionInfo 1 end]
}
}