ad_arg_parser (public)

 ad_arg_parser allowed_args argv

Defined in packages/acs-bootstrap-installer/tcl/00-proc-procs.tcl

Parses an argument list for a database call (switches at the end). Switch values are placed in corresponding variable names in the calling environment. GN: This function is a hack to overcome shortcomings of argument parsing at the first place, since the old-style argument parser of OpenACS does NOT support non-positional arguments after the positional ones. Since the advent of XOTcl2, this limitation is gone and just used for installations without XOTcl.... but since XOTcl is required since many years, this is not an issue anymore.

Parameters:
allowed_args (required)
a list of allowable switch names.
argv (required)
a list of command-line options. May end with args to indicate that extra values should be tolerated after switches and placed in the args list.
Error:
if the list of command-line options is not valid.

Partial Call Graph (max 5 caller/called nodes):
%3 test_ad_arg_parser ad_arg_parser (test acs-bootstrap-installer) ad_arg_parser ad_arg_parser test_ad_arg_parser->ad_arg_parser ad_page_contract ad_page_contract (public) ad_page_contract->ad_arg_parser db_blob_get db_blob_get (public) db_blob_get->ad_arg_parser db_blob_get_file db_blob_get_file (public) db_blob_get_file->ad_arg_parser db_dml db_dml (public) db_dml->ad_arg_parser db_foreach db_foreach (public) db_foreach->ad_arg_parser

Testcases:
ad_arg_parser
Source code:
    #ns_log notice "ad_arg_parser '$allowed_args' <$argv>"

    if {[lindex $allowed_args end] eq "args"} {
        set varargs_p 1
        set allowed_args [lrange $allowed_args 0 [llength $allowed_args]-2]
    } else {
        set varargs_p 0
    }

    if { $varargs_p } {
        upvar args args
        set args [list]
    }

    set counter 0
    foreach { switch value } $argv {
        if { [string index $switch 0] ne "-" } {
            if { $varargs_p } {
            set args [lrange $argv $counter end]
            return
            }
            return -code error "Expected switch but encountered \"$switch\""
        }
        set switch [string range $switch 1 end]
        if { [lsearch $allowed_args $switch] < 0 } {
            return -code error "Invalid switch -$switch (expected one of -[join $allowed_args ", -"])"
        }
        upvar $switch switch_var
        set switch_var $value
        incr counter 2
    }
    if { [llength $argv] % 2 != 0 } {
        # The number of arguments has to be even!
        return -code error "Invalid switch syntax - no argument to final switch \"[lindex $argv end]\""
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: