
proc APSParseArgumentsHelp {} {
    return "Usage: APSParseArguments <optionKeywordList>\n\
<optionKeywordList> is a list of option keywords that are accepted\n\
by the procedure requesting the parsing.  The parsing scans the args\n\
list of the calling procedure.  Only options from the keyword list\n\
will be processed; others are left in the args list.\n\
The effect of APSParseArguments is to translate a sequence like\n\
\"-keyword value\" into \"set keyword value\"; that is, the keyword\n\
names are variable names in the calling procedure.\n"
}

proc APSParseArguments {optlist} {
    upvar args arguments
    set length [llength $arguments]
    set index 0
    set leftovers {}
    while {$index<$length} {
        set arg [lindex $arguments $index]
        if {[string index $arg 0]=="-"} {
            set keywordName [string range $arg 1 end]
            if {[lsearch -exact $optlist $keywordName]!=-1} {
                incr index
                if {$index==$length} {
                    lappend leftovers $arg
                } else {
                    set valueString [lindex $arguments $index]
                    uplevel "set $keywordName {$valueString}"
                    incr index
                }
            } else {
                incr index
                lappend leftovers $arg
            }
        } else {
            lappend leftovers $arg
            incr index
        }
    }
    set arguments [concat $leftovers]
}

proc APSStrictParseArgumentsHelp {} {
    return "Usage: APSStrictParseArguments <optionKeywordList>\n\
Same as APSParseArguments, only it prints an error to stderr if user\n\
passes extra or unknown options not given in optlist. Also, returns\n\
-1 if unknown option given, 0 otherwise.\n\
<optionKeywordList> is a list of option keywords that are accepted\n\
by the procedure requesting the parsing.  The parsing scans the args\n\
list of the calling procedure.  Only options from the keyword list\n\
will be processed; others are left in the args list.\n\
The effect of APSParseArguments is to translate a sequence like\n\
\"-keyword value\" into \"set keyword value\"; that is, the keyword\n\
names are variable names in the calling procedure.\n"
}

proc APSStrictParseArguments {optlist} {
    upvar args arguments
    set length [llength $arguments]
    set index 0
    set leftovers {}
    while {$index<$length} {
        set arg [lindex $arguments $index]
        if {[string index $arg 0]=="-"} {
            set keywordName [string range $arg 1 end]
            if {[lsearch -exact $optlist $keywordName]!=-1} {
                incr index
                if {$index==$length} {
                    lappend leftovers $arg
                } else {
                    set valueString [lindex $arguments $index]
                    uplevel "set $keywordName {$valueString}"
                    incr index
                }
            } else {
                incr index
                lappend leftovers $arg
            }
        } else {
            lappend leftovers $arg
            incr index
        }
    }
    set arguments [concat $leftovers]
    if {$arguments != ""} {
        set procName [lindex [info level [expr {[info level] - 1}]] 0]
        puts stderr "Unknown option(s) given to $procName \"$arguments\""
        if {[info level] > 1} {
            global apsNetworkDomain
            if {($apsNetworkDomain == "aps4.anl.gov")} {
                catch {APSSendEMail -address "soliday@anl.gov" -subject "APSStrictParseArguments warning" -message "[info script]\nUnknown option(s) given to $procName \"$arguments\""}
            }
        }
        return -1
    } else {
        return 0
    }
}


proc APSClearOptions {} {
    upvar args arguments
    set length [llength $arguments]
    set index 0
    set leftovers {}
    while {$index<$length} {
	set arg [lindex $arguments $index]
	if {[string index $arg 0]=="-"} {
	    incr index 2
	} else {
	    lappend leftovers $arg
	    incr index
	}
    }
    set arguments [concat $leftovers]
}



