# Proposition de Package d'analyse d'argument :


# 16/09/01 (diam) rèorganisation
# 22/08/00 (diam) intégration à stead et passage en version 0.2

package provide parsarg 1.0



## Parsarg -- analyse d'option en fonction de specifs rapide
# 
## Parsarg_fastSpec -- analyse d'option (liste paire finale)
#
#   do the following verifications :
#     - list to parse should be pair
#     - option in list to parse should exists in default spec list
#
# Arguments:
#   arrayName       array name to fill with parsed informations
#   argSpecs        argument specifications 
#                   (pair list of -option defaultValue...)
# 
# exemple:
#   dafman::parsarg opt [subst {
#       -colseparator   "\t"
#       -fast           1
#   } 
#
# modif :
#   15/09/00 diam : guess if one should use args or argv and suppress 
#       the "argList" argument
#   22/08/01 : intégration au package parsarg
#   22/09/01 : correction : " if {[info level] == 1} " => traitement de argv
#               avant : le test etait " if {[info level] == 0} "
# 
proc Parsarg_fastSpec {arrayName argSpecs} {
    
    # La liste à analyser :
    #    Attention : on veut détecter "[info level] == 0" du **parent** !
    if {[info level] == 1} {
        upvar argv argList
    } else {
        upvar args argList
    }
    # La tableau d'options à remplir
    upvar $arrayName   options
    
    # 1 - filling default values would arg specification AND verify that 
    #     the argSpecs is a pair list
    #     (this will (??) be more complicated that the following first line)
    array set defOptions $argSpecs 
    array set options   [array get defOptions]

    # 2 - verif that the arg list is pair
    if {[llength $argList] % 2} {
        error "argument list should be pair!\n$argList"
    }
    
    # 3 - read command line
    array set options $argList
    
    # 4 - verify that no unallowed options are used
    if {[llength [array names defOptions]] != [llength [array names options]]} {
        set msg "invalid option name in: $argList\n"
        append msg "allowed options are: [lsort [array names defOptions]]"
        error "$msg"
    }
    # 6 - if argl is "argv", one should update the argc global
    if {[info level] == 1} {
        uplevel #0 [list set argc [llength $argl]]
    }
    
    
}
#./
