File: GetOpts.tcl

package info (click to toggle)
openswan 1%3A2.4.6%2Bdfsg.2-1.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 25,000 kB
  • ctags: 16,877
  • sloc: ansic: 121,112; sh: 19,782; xml: 9,699; asm: 4,422; perl: 4,087; makefile: 3,367; tcl: 713; exp: 657; yacc: 396; pascal: 328; lex: 289; sed: 265; awk: 124; lisp: 3
file content (395 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
## tclGetOpts - GetOpts.tcl
## 
## Description
## 	Tcl package: GetOpts v1.1
## 	Procedures: getopt, typedopts
## 	Contact: Ross Mohn, RPMohn@panix.com
## 	Website: http://www.waxandwane.com/toolbox/tclGetOpts/
## 
## 	tclGetOpts contains the Tcl package GetOpts which
## 	includes two procedures for parsing command-line options in a
## 	Tcl script.
## 
## 		* getopt: a close emulation of the C library routine
## 		getopt(3C).
## 
## 		* typedopts: uses long option names and does type checking
## 		on option arguments.
## 
## 
## Directory Listing
## 	GetOpts.tcl, pkgIndex.tcl
## 	index.html / readme.txt
## 	getopt.html / getopt.txt
## 	typedopts.html / typedopts.txt
## 
## 
## Copyright Information
## 	All copyrights of this package are hereby transferred to Ross
## 	Mohn. This package was originally written by Johnson Earls.
## 
## 
## Version History
## 	v1.1
## 		* tclGetOpts1.1.tar.gz
## 		* Created package GetOpts providing both procedures getopt
## 		and typedopts.
## 		* Fixed bug with the -noinit flag that caused the script
## 		to fail.
## 		* Fixed bug with the list-of option type that would only
## 		return the last option instead of the entire list.
## 
## 	v1.0
## 		* tclGetOpts1.0.tar.gz
## 

package provide GetOpts 1.1

set optind 0
set optindc 0

proc getopt { argslist optstring optret argret } {
  global optind optindc
  upvar $optret retvar
  upvar $argret optarg

# default settings for a normal return
  set optarg ""
  set retvar ""
  set retval 0

# check if we're past the end of the args list
  if { $optind < [ llength $argslist ] } then {

# if we got -- or an option that doesn't begin with -, return (skipping
# the --).  otherwise process the option arg.
    switch -glob -- [ set arg [ lindex $argslist $optind ]] {
      "--" {
        incr optind
      }

      "-*" {
        if { $optindc < 1 } then {
          set optindc 1
        }

        set opt [ string index $arg $optindc ]

        if { [ incr optindc ] == [ string length $arg ] } then {
          set arg [ lindex $argslist [ incr optind ]]
          set optindc 0
        }

        if { [ string match "*$opt*" $optstring ] } then {
          set retvar $opt
          set retval 1
          if { [ string match "*$opt:*" $optstring ] } then {
            if { $optind < [ llength $argslist ] } then {
              set optarg [ string range $arg $optindc end ]
              incr optind
              set optindc 0
            } else {
              set optarg "Option requires an argument -- $opt"
              set retvar $optarg
              set retval -1
            }
          }
        } else {
          set optarg "Illegal option -- $opt"
          set retvar $optarg
          set retval -1
        }
      }
    }
  }

  return $retval
}

proc typedopts { args } {

  proc abbr { s1 s2 } {
    if { [ set len [ string length $s1 ]] } then {
      if { ! [ string compare $s1 [ string range $s2 0 [ expr $len - 1 ]]] } then {
        return 1
      }
    }
    return 0
  }

  proc findabbr { list val } {
    set list [ lsort $list ]
    if { [ set pos [ lsearch -exact $list $val ]] > -1 } then {
      return [ lindex $list $pos ]
    }
    if { [ set pos [ lsearch -glob $list "$val*" ]] > -1 } then {
      if { [ abbr $val [ set realval [ lindex $list $pos ]]] } then {
        if { ! [ abbr $val [ lindex $list [ incr pos ]]] } then {
          return $realval
        }
      }
    }
    return ""
  }

  proc shift { listname } {
    upvar $listname list
    set ret [ lindex $list 0 ]
    set list [ lrange $list 1 end ]
    return $ret
  }

  proc extract { list args } {
    foreach arg $args {
      upvar $arg var
      set var [ shift list ]
    }
    return $list
  }

  proc parseFormats { fmts var } {
    foreach fmt $fmts {
      if { [ regexp $fmt $var ] } then {
        return 1
      }
    }
    return 0
  }

  proc parseOptionType { type listname retname } {
    upvar $listname args
    upvar $retname var

    set ifmt {
      "^\[+-\]?0x\[0-9a-fA-F\]+\$"
      "^\[+-\]?0\[0-7\]+\$"
      "^\[+-\]?\[0-9\]+\$"
    }

    set ffmt {
      "^\[+-\]?\.\[0-9\]+(\[Ee\]\[+-\]?\[0-9\]*)?\$"
      "^\[+-\]?\[0-9\]+\.\[0-9\]*(\[Ee\]\[+-\]?\[0-9\]*)?\$"
      "^\[+-\]?\[0-9\]+\[Ee\]\[+-\]?\[0-9\]*\$"
    }

    set nfmt [ concat $ifmt $ffmt ]

    set otype $type
    switch -exact [ shift type ] {
      b {
        set var ""
        return 1
      }
      i {
        if { [ llength $args ] } then {
          set val [ shift args ]
          if { [ parseFormats $ifmt $val ] } then {
            set var $val
            return 1
          }
        }
        set var "requires an integer argument."
        return 0
      }
      f {
        if { [ llength $args ] } then {
          set val [ shift args ]
          if { [ parseFormats $ffmt $val ] } then {
            set var $val
            return 1
          }
        }
        set var "requires a floating-point argument."
        return 0
      }
      n {
        if { [ llength $args ] } then {
          set val [ shift args ]
          if { [ parseFormats $nfmt $val ] } then {
            set var $val
            return 1
          }
        }
        set var "requires a numeric argument."
        return 0
      }
      s {
        if { [ llength $args ] } then {
          set var [ shift args ]
          return 1
        }
        set var "requires a string argument."
        return 0
      }
      o {
        if { [ llength $args ] } then {
          if { [ string length [ set val [ findabbr $type [ shift args ]]]] } then {
            set var $val
            return 1
          }
        }
        set var "requires a string argument."
        return 0
      }
      m {
        return [ parseOptionType $type args var ]
      }
      l {
        set val ""
        while { [ llength $args ] && ! [ string match "-*" $args ] } {
          if { ! [ parseOptionType $type args ret ] } then {
            set var $ret
            return 0
          }
          lappend val $ret
        }
        set var $val
        return 1
      }
      default {
        puts stderr "Eek!  Option type <$otype> not supported yet!"
        set var "isn't a supported type."
        return 0
      }
    }
  }

  proc parseOption { optlist } {
    set type [ shift optlist ]

    switch -exact [ findabbr { "booleans" "integers" "numbers" "floats" "strings" "one-of" "list-of" "multiple" } $type ] {
      "booleans" -
      "integers" -
      "numbers" -
      "floats" -
      "strings" {
        if { [ llength $optlist ] } then {
          puts stderr "typedopts:  Type $type doesn't take arguments"
          return ""
        }
        return [ string index $type 0 ]
      }
      "one-of" {
        if { ! [ llength $optlist ] } then {
          puts stderr "typedopts:  No arguments given to type $type"
          return ""
        }
        return [ concat [ string index $type 0 ] $optlist ]
      }
      "list-of" -
      "multiple" {
        if { ! [ llength $optlist ] } then {
          puts stderr "typedopts:  No arguments given to type $type"
          return ""
        }
        if { ! [ string length [ set subtype [ parseOption $optlist ]]] } then {
          return ""
        }
        return [ concat [ string index $type 0 ] $subtype ]
      }
      default {
        puts stderr "typedopts:  Unknown option type $type"
        return ""
      }
    }
  }

  set doinit 1

  if { [ llength $args ] < 5 } then {
    puts stderr "typedopts: bad number of arguments."
    return -1
  }

  set args [ extract $args arglist optlist optret argret restret ]

  while { [ llength $args ] } {
    set opt [ shift args ]
    switch -exact -- [ findabbr { -noinitialize } $opt ] {
      -noinitialize {
        set doinit 0
      }
      default {
        puts stderr "typedopts: bad option \"$opt\": should be -noinitialize or --"
        return -1
      }
    }
  }

  upvar $optret _opts
  upvar $argret _args
  upvar $restret _rest

  set allopts ""

  set type ""

  foreach word $optlist {
    set word [ string trim $word ]
    if { [ string length $type ] } then {
      foreach arg $word {
        if { [ lsearch -exact $arg $allopts ] > -1 } then {
          puts stderr "typedopts: option -$arg multiply declared."
          return -1
        }
        lappend allopts $arg
        set opttype($arg) $type
      }
      set type ""
    } else {
      if { ! [ string length [ set type [ parseOption $word ]]] } then {
        return -1
      }
    }
  }

  if { $doinit } then {
    foreach opt $allopts {
      set _opts($opt) 0
      set _args($opt) ""
    }
  }

set _args(_ERROR_) ""

  set retval 1

  while { [ llength $arglist ] } {
    switch -glob -- $arglist {
      -- {
        shift arglist
        break
      }
      -* {
      }
      * {
        break
      }
    }
    set opt [ string range [ shift arglist ] 1 end ]
    if { [ string length [ set fnd [ findabbr $allopts $opt ]]] } then {
      set type $opttype($fnd)
      if { [ parseOptionType $opttype($fnd) arglist arg ] } then {
        if { $_opts($fnd) && ! [ string match "m*" $type ] } then {
          set _args(_ERROR_) "Found multiple occurrences of option -$fnd"
          set retval 0
          break
        }
        set _opts($fnd) 1
        lappend _args($fnd) $arg
      } else {
        set _args(_ERROR_) "Option -$fnd $arg"
        set retval 0
        break
      }
    } else {
      set _args(_ERROR_) "Unknown option: -$opt"
      set retval 0
      break
    }
  }

  set _rest $arglist

  return $retval
}