source: trunk/gui/scripts/getopts.tcl @ 16

Last change on this file since 16 was 11, checked in by mmc, 19 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 4.1 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: getopts - simple argument parser
3#
4#  This utility makes it easy to process arguments for various
5#  commands.  You give a description of arguments, and it will
6#  parse and return the values.
7#
8#    getopts args params {
9#        value -foo foo_default
10#        flag group -bar default
11#        flag group -baz
12#    }
13#
14#  Note that both args and params are passed by name, not by
15#  value (not $args or $params).  This function initializes the
16#  params variable according to the values in the spec list.
17#  It then loops through all args, matching as many as possible
18#  from the list.  It throws an error on the first error
19#  encountered.  If there are extra arguments that don't match
20#  the flags and don't start with -, they are left in args, and
21#  processing stops.
22#
23# ======================================================================
24#  AUTHOR:  Michael McLennan, Purdue University
25#  Copyright (c) 2004-2005
26#  Purdue Research Foundation, West Lafayette, IN
27# ======================================================================
28namespace eval Rappture { # forward declaration }
29
30# ----------------------------------------------------------------------
31# USAGE: getopts <listvar> <returnvar> <spec>
32#
33# Processes options in <listvar>, storing results in <returnvar>.
34# Throws an exception if an error is encountered.  Leaves any remaining
35# arguments (after flags) in <listvar>.
36# ----------------------------------------------------------------------
37proc Rappture::getopts {listVar returnVar spec} {
38    upvar $listVar args
39    upvar $returnVar params
40    catch {unset params}
41    set opts ""
42
43    #
44    # Pick apart the info in the <spec> and set up flags/params
45    #
46    foreach line [split $spec \n] {
47        if {[llength $line] == 0} {
48            continue  ;# ignore blank lines
49        }
50
51        set type [lindex $line 0]
52        switch -- $type {
53            value {
54                if {[llength $line] < 3} {
55                    error "bad value spec \"$line\": should be \"value -flag default\""
56                }
57                set name [lindex $line 1]
58                set flags($name) $type
59                set params($name) [lindex $line 2]
60                lappend opts $name
61            }
62            flag {
63                if {[llength $line] < 3 || [llength $line] > 4} {
64                    error "bad value spec \"$line\": should be \"flag group -flag ?default?\""
65                }
66                set group [lindex $line 1]
67                set name [lindex $line 2]
68                set flags($name) [list $type $group]
69                if {[llength $line] > 3} {
70                    set params($group) $name
71                    set params($name) 1
72                } else {
73                    if {![info exists params($group)]} {
74                        set params($group) ""
75                    }
76                    set params($name) 0
77                }
78                lappend opts $name
79            }
80            default {
81                error "bad arg type \"$type\": should be flag or value"
82            }
83        }
84    }
85
86    #
87    # Now, walk through the values in $args and extract parameters.
88    #
89    while {[llength $args] > 0} {
90        set first [lindex $args 0]
91        if {[string index $first 0] != "-"} {
92            break
93        }
94        if {"--" == $first} {
95            set args [lrange $args 1 end]
96            break
97        }
98        if {![info exists params($first)]} {
99            error "bad option \"$first\": should be [join [lsort $opts]] {, }]"
100        }
101        switch -- [lindex $flags($first) 0] {
102            value {
103                if {[llength $args] < 2} {
104                    error "missing value for option $first"
105                }
106                set params($first) [lindex $args 1]
107                set args [lrange $args 2 end]
108            }
109            flag {
110                set group [lindex $flags($first) 1]
111                set params($group) $first
112                set params($first) 1
113                set args [lrange $args 1 end]
114            }
115        }
116    }
117    return ""
118}
Note: See TracBrowser for help on using the repository browser.