TABLE OF CONTENTS
::pwtk::parseOpt_
SYNOPSIS
proc ::pwtk::parseOpt_ {} {
DESCRIPTION
A convenience routine for parsing options for procs that are of "proc name {args} {...}" form, where the usage is as:
name -opt1 -opt2 val -opt3 -val arg1 arg2 arg3 ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ | | +--- options and their values + extra agruments that follow after options
BEWARE
In addition to the args variable, the following variables must also be defined to use this proc:
- options ... option specs for ::cmdline::getoptions
- usage ... usage string to print for "usage" error
and either:
- narg ... number of arguments that follow the options (in above example narg == 3); if narg < 0 then no checking is performed
or:
- nargmin, nargmax ... min and max number of allowed arguments that follow the options; if nargmax < 0 then no checking is performed
The parsed opions are stored in "opt" array.
SOURCE
uplevel 1 { foreach var {args options usage} { if { ! [info exists $var] } { ::error "coding error: variable $var does not exist" } } set args_ $args array set opt [::cmdline::getoptions args $options "error while executing:\n\n[pwtk::procName] $args_\n\n[::pwtk::procName] options:"] if { [info exists narg] } { if { $narg >= 0 && [llength $args] != $narg } { # nargs ::pwtk::error "wrong # args, should be:\n\n[::pwtk::procName] $usage\n\nbut got: [::pwtk::procName] $args_" 1 } } elseif { [info exists nargmin] && [info exists nargmax] } { # nargmin nargmax if { [llength $args] < $nargmin } { ::pwtk::error "wrong # args, should be:\n\n[::pwtk::procName] $usage\n\nbut got: [::pwtk::procName] $args_" 1 } if { $nargmax >= 0 && [llength $args] > $nargmax } { ::pwtk::error "wrong # args, should be: \n\n[::pwtk::procName] $usage\n\nbut got: [::pwtk::procName] $args_" 1 } # if nargmax < 0 ==> no checking } else { ::error "coding error: neither narg nor nargmin & nargmax exist" } } }