getopt-declare 1.25 → 1.26
Sign up to get free protection for your applications and to get access to all the features.
- data/Declare.rdoc +20 -6
- data/HISTORY.txt +10 -0
- data/Rakefile +4 -0
- data/lib/Getopt/Declare.rb +1739 -1738
- data/test/test_cmdline_array.rb +3 -5
- metadata +2 -2
data/Declare.rdoc
CHANGED
@@ -1184,6 +1184,25 @@
|
|
1184
1184
|
# second Getopt::Declare object).
|
1185
1185
|
#
|
1186
1186
|
#
|
1187
|
+
# == Other global directives
|
1188
|
+
#
|
1189
|
+
# Getopt::Declare offers some other less used global directives to control
|
1190
|
+
# how some options are handled.
|
1191
|
+
#
|
1192
|
+
# The directives are:
|
1193
|
+
#
|
1194
|
+
# * <tt>[tight]</tt>
|
1195
|
+
#
|
1196
|
+
# Specifies that the usage (help) description should be printed in a tight
|
1197
|
+
# form, eliminating as many empty or whitespace only lines as possible.
|
1198
|
+
#
|
1199
|
+
# * <tt>[debug]</tt>
|
1200
|
+
#
|
1201
|
+
# For debugging purposes, it specifies that the internal parser that
|
1202
|
+
# Getopt::Declare generates gets saved to a file called .CODE.rb.
|
1203
|
+
# This file is placed in the directory where the command was run from.
|
1204
|
+
#
|
1205
|
+
#
|
1187
1206
|
# == Parameter dependencies
|
1188
1207
|
#
|
1189
1208
|
# Getopt::Declare provides five other directives which modify the
|
@@ -1200,13 +1219,8 @@
|
|
1200
1219
|
# condition was violated, the program terminates
|
1201
1220
|
# (whilst still inside Getopt::Declare::new()).
|
1202
1221
|
#
|
1203
|
-
# The directives are:
|
1204
|
-
#
|
1205
|
-
# * <tt>[tight]</tt>
|
1206
|
-
#
|
1207
|
-
# Specifies that the usage description should be printed in a tight
|
1208
|
-
# form, eliminating as many empty or whitespace only lines as possible.
|
1209
1222
|
#
|
1223
|
+
# The directives are:
|
1210
1224
|
#
|
1211
1225
|
# * <tt>[required]</tt>
|
1212
1226
|
#
|
data/HISTORY.txt
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
== 1.26
|
2
|
+
* Stripping of usage was making help lines that were not
|
3
|
+
parameters look bad. Fixed.
|
4
|
+
* [ requires: param && flag ] was being expanded incorrectly,
|
5
|
+
leading to an obtuse error. Fixed.
|
6
|
+
like what user expects.
|
7
|
+
* Removed _enbool() routine as it is no longer needed.
|
8
|
+
* Documented [debug] directive.
|
9
|
+
* Updated usage() tests.
|
10
|
+
|
1
11
|
== 1.25
|
2
12
|
* Really just v1.24, but there were problems with hoe releasing.
|
3
13
|
* Made -version not fail when doing rake tests and file is not
|
data/Rakefile
CHANGED
@@ -8,6 +8,10 @@ require './lib/Getopt/Declare'
|
|
8
8
|
rdoc_files = ["Declare.rdoc", 'HISTORY.txt'] + Dir.glob('lib/*/*.rb')
|
9
9
|
|
10
10
|
|
11
|
+
#
|
12
|
+
# Make sure that ~/.rubyforge/auto-config.yml contains getopt-declare
|
13
|
+
# as project (not getoptdeclare).
|
14
|
+
#
|
11
15
|
Hoe.new('getopt-declare', Getopt::Declare::VERSION) do |p|
|
12
16
|
p.rubyforge_name = name = 'getoptdeclare'
|
13
17
|
p.author = "Gonzalo Garramuno"
|
data/lib/Getopt/Declare.rb
CHANGED
@@ -1,1738 +1,1739 @@
|
|
1
|
-
#
|
2
|
-
# Getopt::Declare - Declaratively Expressed Command-Line Arguments via Regular Expressions
|
3
|
-
#
|
4
|
-
# Ruby port of Perl's Getopt::Declare, version 1.21,
|
5
|
-
# released May 21, 1999.
|
6
|
-
#
|
7
|
-
# $Release Version: 1.
|
8
|
-
# $Date: 2007/01/15 10:53:09 $
|
9
|
-
# by Gonzalo Garramu�o
|
10
|
-
#
|
11
|
-
# For detailed instructions, see Declare.rdoc file
|
12
|
-
#
|
13
|
-
# Ruby Port:
|
14
|
-
# Copyright (c) 2004, Gonzalo Garramuno. All Rights Reserved.
|
15
|
-
# This package is free software. It may be used, redistributed
|
16
|
-
# and/or modified under the terms of the Perl Artistic License
|
17
|
-
# (see http://www.perl.com/perl/misc/Artistic.html)
|
18
|
-
#
|
19
|
-
# Original Perl Implementation:
|
20
|
-
# Damian Conway (damian@conway.org)
|
21
|
-
# Copyright (c) 1997-2000, Damian Conway. All Rights Reserved.
|
22
|
-
# This package is free software. It may be used, redistributed
|
23
|
-
# and/or modified under the terms of the Perl Artistic License
|
24
|
-
# (see http://www.perl.com/perl/misc/Artistic.html)
|
25
|
-
#
|
26
|
-
|
27
|
-
require "Getopt/DelimScanner"
|
28
|
-
|
29
|
-
|
30
|
-
# Verifies that code is valid Ruby code. returns false if not
|
31
|
-
def valid_syntax?(code, fname = 'parser_code')
|
32
|
-
eval("BEGIN {return true}\n#{code}", nil, fname, 0)
|
33
|
-
rescue Exception
|
34
|
-
false
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
# An add-on to the String class
|
39
|
-
class String
|
40
|
-
# Expand all tabs to spaces
|
41
|
-
def expand_tabs!( tabstop = 8 )
|
42
|
-
while self.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
|
43
|
-
val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
|
44
|
-
"#$1#$2" + (" " * val)
|
45
|
-
}
|
46
|
-
end
|
47
|
-
self
|
48
|
-
end
|
49
|
-
|
50
|
-
# Return new string with all tabs set to spaces
|
51
|
-
def expand_tabs( tabstop = 8 )
|
52
|
-
h = self.dup
|
53
|
-
while h.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
|
54
|
-
val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
|
55
|
-
"#$1#$2" + (" " * val)
|
56
|
-
}
|
57
|
-
end
|
58
|
-
h
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
|
63
|
-
# Regex for removing bracket directives
|
64
|
-
BracketDirectives =
|
65
|
-
/\[\s*(?:ditto|tight|strict|no\s*case|repeatable|debug|required|mutex:.*|implies:.*|excludes:.*|requires:.*|cluster:.*)\s*\]/
|
66
|
-
|
67
|
-
|
68
|
-
module Getopt
|
69
|
-
|
70
|
-
# Main Class
|
71
|
-
class Declare
|
72
|
-
|
73
|
-
VERSION = '1.
|
74
|
-
|
75
|
-
# For debugging, use [debug] and it will output the ruby code as .CODE.rb
|
76
|
-
@@debug = false
|
77
|
-
|
78
|
-
# Main separator used to distinguish arguments in Getopt/Declare spec.
|
79
|
-
# By default, one or more tabs or 3 spaces or more.
|
80
|
-
@@separator = '(?:\t+| {3})'
|
81
|
-
|
82
|
-
# Class used to handle the beginning of options
|
83
|
-
class StartOpt
|
84
|
-
|
85
|
-
# Returns regex used matching start of options
|
86
|
-
def matcher(g)
|
87
|
-
'(?:()'
|
88
|
-
end
|
89
|
-
|
90
|
-
# Returns code used
|
91
|
-
def code(*t)
|
92
|
-
''
|
93
|
-
end
|
94
|
-
|
95
|
-
# Returns how to cache code in class
|
96
|
-
def cachecode(a,b)
|
97
|
-
''
|
98
|
-
end
|
99
|
-
|
100
|
-
# Helps build regex that matches parameters of flags
|
101
|
-
def trailer
|
102
|
-
nil
|
103
|
-
end
|
104
|
-
|
105
|
-
# Helps build regex that matches parameters of flags
|
106
|
-
def ows(g)
|
107
|
-
g
|
108
|
-
end
|
109
|
-
end # StartOpt
|
110
|
-
|
111
|
-
|
112
|
-
# Class used to handle the ending of options
|
113
|
-
class EndOpt < StartOpt
|
114
|
-
# Returns regex used matching end of options
|
115
|
-
def matcher(g)
|
116
|
-
'())?'
|
117
|
-
end
|
118
|
-
end # EndOpt
|
119
|
-
|
120
|
-
|
121
|
-
# Class used to handle scalar (ie.non-array) parameters
|
122
|
-
class ScalarArg
|
123
|
-
|
124
|
-
@@stdtype = {}
|
125
|
-
|
126
|
-
# (re)set standard types
|
127
|
-
def ScalarArg._reset_stdtype
|
128
|
-
@@stdtype = {
|
129
|
-
':i' => { 'pattern' => '(?:(?:%T[+-]?)%D+)' },
|
130
|
-
':n' => { 'pattern' => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
|
131
|
-
'(?:%T[eE](?:[+-])?%D+)?|%T\.%D+(?:%T[eE](?:[+-])?%D+)?))',
|
132
|
-
},
|
133
|
-
':s' => { 'pattern' => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
|
134
|
-
':qs' => { 'pattern' => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
|
135
|
-
':id' => { 'pattern' => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
|
136
|
-
':d' => { 'pattern' => '(?:%T(?:\S|\0))+',
|
137
|
-
'action' => %q%
|
138
|
-
reject( (_VAL_.nil? || !test(?d, _VAL_) ),
|
139
|
-
"in parameter '#{_PARAM_}' (\"#{_VAL_}\" is not a directory)")%
|
140
|
-
},
|
141
|
-
':if' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
|
142
|
-
'action' => %q%
|
143
|
-
reject( (_VAL_.nil? || _VAL_ != "-" && !test(?r, _VAL_) ),
|
144
|
-
"in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not readable)")%
|
145
|
-
},
|
146
|
-
':of' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
|
147
|
-
'action' => %q%
|
148
|
-
reject( (_VAL_.nil? || _VAL_ != "-" &&
|
149
|
-
test(?r, _VAL_) && !test(?w, _VAL_)),
|
150
|
-
"in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not writable)")%
|
151
|
-
},
|
152
|
-
'' => { 'pattern' => ':s', 'ind' => 1 },
|
153
|
-
|
154
|
-
':+i' => { 'pattern' => ':i',
|
155
|
-
'action' => %q%reject( _VAL_ <= 0,
|
156
|
-
"in parameter '#{_PARAM_}' (#{_VAL_} must be an integer greater than zero)")%,
|
157
|
-
'ind' => 1
|
158
|
-
},
|
159
|
-
|
160
|
-
':+n' => { 'pattern' => ':n',
|
161
|
-
'action' => %q%reject( _VAL_ <= 0.0,
|
162
|
-
"in parameter '#{_PARAM_}' (#{_VAL_} must be a number greater than zero)")%,
|
163
|
-
'ind' => 1
|
164
|
-
},
|
165
|
-
|
166
|
-
':0+i' => { 'pattern' => ':i',
|
167
|
-
'action' => %q%reject( _VAL_ < 0,
|
168
|
-
"in parameter '#{_PARAM_}' (#{_VAL_} must be an positive integer)")%,
|
169
|
-
'ind' => 1
|
170
|
-
},
|
171
|
-
|
172
|
-
':0+n' => {
|
173
|
-
'pattern' => ':n',
|
174
|
-
'action' => %q%reject( _VAL_ < 0,
|
175
|
-
"in parameter '#{_PARAM_}' (#{_VAL_} must be an positive number)")%,
|
176
|
-
'ind' => 1
|
177
|
-
},
|
178
|
-
}
|
179
|
-
|
180
|
-
end # _reset_stdtype
|
181
|
-
|
182
|
-
|
183
|
-
# Given a standard type name, return the corresponding regex
|
184
|
-
# pattern or nil
|
185
|
-
def ScalarArg.stdtype(name)
|
186
|
-
seen = {}
|
187
|
-
while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
|
188
|
-
seen[name] = 1; name = @@stdtype[name]['pattern']
|
189
|
-
end
|
190
|
-
|
191
|
-
return nil if seen[name] || !@@stdtype[name]
|
192
|
-
@@stdtype[name]['pattern']
|
193
|
-
end
|
194
|
-
|
195
|
-
def stdtype(name)
|
196
|
-
ScalarArg.stdtype(name)
|
197
|
-
end
|
198
|
-
|
199
|
-
|
200
|
-
# Given the name of a type, return its corresponding action(s)
|
201
|
-
def ScalarArg.stdactions(name)
|
202
|
-
seen = {}
|
203
|
-
actions = []
|
204
|
-
while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
|
205
|
-
seen[name] = 1
|
206
|
-
if @@stdtype[name]['action']
|
207
|
-
actions.push( @@stdtype[name]['action'] )
|
208
|
-
end
|
209
|
-
name = @@stdtype[name]['pattern']
|
210
|
-
end
|
211
|
-
|
212
|
-
if @@stdtype[name] && @@stdtype[name]['action']
|
213
|
-
actions.push( @@stdtype[name]['action'] )
|
214
|
-
end
|
215
|
-
|
216
|
-
return actions
|
217
|
-
end
|
218
|
-
|
219
|
-
# Add a new (user defined) type to the standard types
|
220
|
-
def ScalarArg.addtype(abbrev, pattern, action, ref)
|
221
|
-
|
222
|
-
typeid = ":#{abbrev}"
|
223
|
-
unless (pattern =~ /\S/)
|
224
|
-
pattern = ":s"
|
225
|
-
ref = 1
|
226
|
-
end
|
227
|
-
|
228
|
-
@@stdtype[typeid] = {}
|
229
|
-
@@stdtype[typeid]['pattern'] = "(?:#{pattern})" if pattern && !ref
|
230
|
-
@@stdtype[typeid]['pattern'] = ":#{pattern}" if pattern && ref
|
231
|
-
@@stdtype[typeid]['action'] = action if action
|
232
|
-
@@stdtype[typeid]['ind'] = ref
|
233
|
-
|
234
|
-
end
|
235
|
-
|
236
|
-
attr :name
|
237
|
-
attr :type
|
238
|
-
attr :nows
|
239
|
-
|
240
|
-
|
241
|
-
# Constructor
|
242
|
-
def initialize(name, type, nows)
|
243
|
-
@name = name
|
244
|
-
@type = type
|
245
|
-
@nows = nows
|
246
|
-
end
|
247
|
-
|
248
|
-
# Create regexp to match parameter
|
249
|
-
def matcher(g)
|
250
|
-
trailing = g ? '(?!'+Regexp::quote(g)+')' : ''
|
251
|
-
|
252
|
-
# Find type in list of standard (and user) types
|
253
|
-
stdtype = stdtype(@type)
|
254
|
-
|
255
|
-
# Handle stdtypes that are specified as regex in parameter
|
256
|
-
if (!stdtype && @type =~ %r"\A:/([^/]+)/\Z" )
|
257
|
-
stdtype = "#$1"
|
258
|
-
end
|
259
|
-
|
260
|
-
if stdtype.nil?
|
261
|
-
raise "Error: bad type in Getopt::Declare parameter variable specification near '<#{@name}#{@type}>'\nValid types are:\n" + @@stdtype.keys.inspect
|
262
|
-
end
|
263
|
-
|
264
|
-
stdtype = stdtype.dup # make a copy, as we'll change it in place
|
265
|
-
stdtype.gsub!(/\%D/,"(?:#{trailing}\\d)")
|
266
|
-
stdtype.gsub!(/\%T/,trailing)
|
267
|
-
unless ( stdtype.sub!("\%F","") )
|
268
|
-
stdtype = Getopt::Declare::Arg::negflagpat + stdtype
|
269
|
-
end
|
270
|
-
return "(?:#{stdtype})"
|
271
|
-
end
|
272
|
-
|
273
|
-
# Return string with code to process parameter
|
274
|
-
def code(*t)
|
275
|
-
if t[0]
|
276
|
-
pos1 = t[0].to_s
|
277
|
-
else
|
278
|
-
pos1 = '0'
|
279
|
-
end
|
280
|
-
|
281
|
-
c = conversion
|
282
|
-
c = "\n _VAL_ = _VAL_#{c} if _VAL_" if c
|
283
|
-
|
284
|
-
code = <<-EOS
|
285
|
-
_VAR_ = %q|<#{@name}>|
|
286
|
-
_VAL_ = @@m[#{pos1}]
|
287
|
-
_VAL_.tr!("\\0"," ") if _VAL_#{c}
|
288
|
-
EOS
|
289
|
-
|
290
|
-
actions = Getopt::Declare::ScalarArg::stdactions(@type)
|
291
|
-
|
292
|
-
for i in actions
|
293
|
-
next if i.nil?
|
294
|
-
# i.sub!(/(\s*\{)/, '\1 module '+t[1])
|
295
|
-
code << "
|
296
|
-
begin
|
297
|
-
#{i}
|
298
|
-
end
|
299
|
-
"
|
300
|
-
end
|
301
|
-
|
302
|
-
code << " #{@name} = _VAL_\n"
|
303
|
-
end
|
304
|
-
|
305
|
-
# Based on parameter type, default conversion to apply
|
306
|
-
def conversion
|
307
|
-
pat = @@stdtype[@type] ? @@stdtype[@type]['pattern'] : ''
|
308
|
-
[ @type, pat ].each { |t|
|
309
|
-
case t
|
310
|
-
when /^\:0?(\+)?i$/
|
311
|
-
return '.to_i'
|
312
|
-
when /^\:0?(\+)?n$/
|
313
|
-
return '.to_f'
|
314
|
-
end
|
315
|
-
}
|
316
|
-
return nil
|
317
|
-
end
|
318
|
-
|
319
|
-
# Return string with code to cache argument in Getopt::Declare's cache
|
320
|
-
def cachecode(ownerflag, itemcount)
|
321
|
-
if itemcount > 1
|
322
|
-
" @cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n"
|
323
|
-
else
|
324
|
-
" @cache['#{ownerflag}'] = #{@name}\n"
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
# Helps build regex that matches parameters of flags
|
329
|
-
def trailer
|
330
|
-
nil # MEANS TRAILING PARAMETER VARIABLE (in Perl,was '')
|
331
|
-
end
|
332
|
-
|
333
|
-
# Helps build regex that matches parameters of flags
|
334
|
-
# Wraps parameter passed for #$1, etc. matching
|
335
|
-
def ows(g)
|
336
|
-
return '[\s|\0]*(' + g + ')' unless @nows
|
337
|
-
'('+ g +')'
|
338
|
-
end
|
339
|
-
|
340
|
-
end # ScalarArg
|
341
|
-
|
342
|
-
|
343
|
-
# Class used to handle array arguments
|
344
|
-
class ArrayArg < ScalarArg
|
345
|
-
|
346
|
-
# Create regexp to match array
|
347
|
-
def matcher(g)
|
348
|
-
suffix = !g.nil? ? '([\s\0]+)' : ''
|
349
|
-
scalar = super # contains regex to match a scalar element
|
350
|
-
# we match one obligatory element, and one or more optionals ')*'
|
351
|
-
return scalar + '(?:[\s\0]+' + scalar + ')*' + suffix
|
352
|
-
end
|
353
|
-
|
354
|
-
# Return string with code to process array parameter
|
355
|
-
def code(*t)
|
356
|
-
|
357
|
-
if t[0]
|
358
|
-
pos1 = t[0].to_s
|
359
|
-
else
|
360
|
-
pos1 = '0'
|
361
|
-
end
|
362
|
-
|
363
|
-
code = <<-EOS
|
364
|
-
_VAR_ = %q|<#{@name}>|
|
365
|
-
_VAL_ = nil
|
366
|
-
#{@name} = (@@m[#{pos1}]||'').split(' ').map { |i|
|
367
|
-
i.tr("\\0", " ") }
|
368
|
-
EOS
|
369
|
-
|
370
|
-
# Handle conversion to proper type
|
371
|
-
c = conversion
|
372
|
-
if c
|
373
|
-
code << " #{@name}.map! { |i| i#{c} }\n"
|
374
|
-
end
|
375
|
-
|
376
|
-
actions = Getopt::Declare::ScalarArg::stdactions(@type)
|
377
|
-
if actions.size > 0
|
378
|
-
code << " for _VAL_ in #{@name}\n"
|
379
|
-
for i in actions
|
380
|
-
code << " #{i}\n"
|
381
|
-
end
|
382
|
-
code << " end\n\n"
|
383
|
-
end
|
384
|
-
return code
|
385
|
-
end
|
386
|
-
|
387
|
-
# Return string with code to cache array in Getopt::Declare's cache
|
388
|
-
def cachecode(ownerflag, itemcount)
|
389
|
-
if itemcount > 1
|
390
|
-
" @cache['#{ownerflag}']['<#{@name}>'] = [] unless @cache['#{ownerflag}']['<#{@name}>']
|
391
|
-
@cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n"
|
392
|
-
else
|
393
|
-
" @cache['#{ownerflag}'] = #{@name}\n"
|
394
|
-
end
|
395
|
-
end
|
396
|
-
end # ArrayArg
|
397
|
-
|
398
|
-
|
399
|
-
# Class used to handle punctuations (., -, etc.)
|
400
|
-
class Punctuator
|
401
|
-
|
402
|
-
# Constructor
|
403
|
-
def initialize(text, nows)
|
404
|
-
@text = text
|
405
|
-
@nows = nows
|
406
|
-
end
|
407
|
-
|
408
|
-
# Return regex that matches this punctuation
|
409
|
-
def matcher(g)
|
410
|
-
Arg::negflagpat + Regexp::quote(@text)
|
411
|
-
end
|
412
|
-
|
413
|
-
# Return string with code to process punctuation
|
414
|
-
def code(*t)
|
415
|
-
|
416
|
-
if t[0]
|
417
|
-
pos1 = t[0].to_s
|
418
|
-
else
|
419
|
-
pos1 = '0'
|
420
|
-
end
|
421
|
-
" if @@m[#{pos1}] && !@@m[#{pos1}].empty?
|
422
|
-
_PUNCT_['#{@text}'] = @@m[#{pos1}]
|
423
|
-
end
|
424
|
-
"
|
425
|
-
end
|
426
|
-
|
427
|
-
# Return string with code to cache punctuation in Getopt::Declare's cache
|
428
|
-
def cachecode(ownerflag, itemcount)
|
429
|
-
if itemcount > 1
|
430
|
-
" @cache['#{ownerflag}']['#{@text}'] = _PUNCT_['#{@text}']\n"
|
431
|
-
else
|
432
|
-
" unless @cache['#{ownerflag}']\n" +
|
433
|
-
" @cache['#{ownerflag}'] = _PUNCT_['#{@text}'] || 1\n" +
|
434
|
-
" end\n"
|
435
|
-
end
|
436
|
-
end
|
437
|
-
|
438
|
-
# Helps build regex that matches parameters of flags
|
439
|
-
def trailer
|
440
|
-
@text
|
441
|
-
end
|
442
|
-
|
443
|
-
# Helps build regex that matches parameters of flags
|
444
|
-
# Wraps parameter passed for #$1, etc. matching
|
445
|
-
def ows(g)
|
446
|
-
return '[\s\0]*(' + g + ')' unless @nows
|
447
|
-
'(' + g + ')'
|
448
|
-
end #ows
|
449
|
-
|
450
|
-
end # Punctuator
|
451
|
-
|
452
|
-
|
453
|
-
# Class used to handle other arguments (flags, etc)
|
454
|
-
class Arg
|
455
|
-
|
456
|
-
@@nextid = 0
|
457
|
-
|
458
|
-
|
459
|
-
Helpcmd = %w( -help --help -Help --Help -HELP --HELP -h -H )
|
460
|
-
|
461
|
-
@@helpcmdH = {}
|
462
|
-
for i in Helpcmd; @@helpcmdH[i] = 1; end
|
463
|
-
|
464
|
-
def Arg.besthelp
|
465
|
-
for i in Helpcmd; return i if @@helpcmdH[i]; end
|
466
|
-
end
|
467
|
-
|
468
|
-
# Create regex of help flags based on help shortcuts left
|
469
|
-
def Arg.helppat
|
470
|
-
@@helpcmdH.keys.join('|')
|
471
|
-
end
|
472
|
-
|
473
|
-
|
474
|
-
Versioncmd = %w( -version --version -Version --Version
|
475
|
-
-VERSION --VERSION -v -V )
|
476
|
-
@@versioncmdH = {}
|
477
|
-
for i in Versioncmd; @@versioncmdH[i] = 1; end
|
478
|
-
|
479
|
-
def Arg.bestversion
|
480
|
-
for i in Versioncmd; return i if @@versioncmdH[i]; end
|
481
|
-
end
|
482
|
-
|
483
|
-
# Create regex of version flags based on help shortcuts left
|
484
|
-
def Arg.versionpat
|
485
|
-
@@versioncmdH.keys.join('|')
|
486
|
-
end
|
487
|
-
|
488
|
-
@@flags = []
|
489
|
-
@@posflagpat = nil
|
490
|
-
@@negflagpat = nil
|
491
|
-
|
492
|
-
def Arg.clear
|
493
|
-
@@flags = []
|
494
|
-
@@nextid = 0
|
495
|
-
@@posflagpat = nil
|
496
|
-
@@negflagpath = nil
|
497
|
-
end
|
498
|
-
|
499
|
-
# Return string with regex that avoids all flags in declaration
|
500
|
-
def Arg.negflagpat(*t)
|
501
|
-
if !@@negflagpat && @@flags
|
502
|
-
@@negflagpat = ( @@flags.map { |i|
|
503
|
-
"(?!" + Regexp::quote(i) + ")" } ).join('')
|
504
|
-
else
|
505
|
-
@@negflagpat
|
506
|
-
end
|
507
|
-
end
|
508
|
-
|
509
|
-
# Return string with regex that matches any of the flags in declaration
|
510
|
-
def Arg.posflagpat(*t)
|
511
|
-
if !@@posflagpat && @@flags
|
512
|
-
@@posflagpat = '(?:' + ( @@flags.map { |i|
|
513
|
-
Regexp::quote(i) } ).join('|') + ')'
|
514
|
-
else
|
515
|
-
@@posflagpat
|
516
|
-
end
|
517
|
-
end
|
518
|
-
|
519
|
-
attr_accessor :flag, :args, :actions, :ditto, :nocase
|
520
|
-
attr_accessor :required, :
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
@
|
537
|
-
@
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
if
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
@
|
592
|
-
end
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
code << '
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
@args.
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
code << "
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
code << "
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
#
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
#
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
if
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
#
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
name =
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
#
|
920
|
-
|
921
|
-
pat
|
922
|
-
pat
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
"
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
originaldesc.
|
952
|
-
originaldesc.gsub!(
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
mref[
|
977
|
-
mref[excluded]
|
978
|
-
mref[
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
#
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
###
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
Getopt::Declare::
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
_action
|
1047
|
-
|
1048
|
-
i.sub!(
|
1049
|
-
|
1050
|
-
_action
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
_action
|
1062
|
-
|
1063
|
-
i.sub!(
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
desc
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
specs
|
1106
|
-
|
1107
|
-
arg
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
arg
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
decorator
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
#
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
(
|
1152
|
-
|
1153
|
-
}
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
(@usage =~ /\[\s*cluster:\s*
|
1160
|
-
(@usage =~ /\[\s*cluster:\s*
|
1161
|
-
(@usage =~ /\[\s*cluster
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
@
|
1167
|
-
@
|
1168
|
-
@
|
1169
|
-
|
1170
|
-
|
1171
|
-
@
|
1172
|
-
@
|
1173
|
-
@
|
1174
|
-
@
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
f.
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
#
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
_args
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
source[0] == "-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
progname
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
source = source[0
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
#
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
t.sub!(
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
t.sub!(
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
desc.gsub!(
|
1367
|
-
desc.gsub!(
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
desc
|
1391
|
-
#desc.gsub!(
|
1392
|
-
|
1393
|
-
|
1394
|
-
desc
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
prog
|
1424
|
-
header << "
|
1425
|
-
header << "
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
if opt.empty?
|
1433
|
-
return header + usage + "\n"
|
1434
|
-
end
|
1435
|
-
|
1436
|
-
pager = $stdout
|
1437
|
-
#begin
|
1438
|
-
# eval('require "IO/Pager";')
|
1439
|
-
# pager = IO::Pager.new()
|
1440
|
-
#rescue
|
1441
|
-
#end
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
@
|
1466
|
-
@
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
undef :
|
1473
|
-
undef :
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
@
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
substr
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
end
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
end
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
end
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
t <<
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
@cache
|
1706
|
-
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
return @cache
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
attr :
|
1725
|
-
attr :
|
1726
|
-
attr :
|
1727
|
-
attr :
|
1728
|
-
attr :
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1
|
+
#
|
2
|
+
# Getopt::Declare - Declaratively Expressed Command-Line Arguments via Regular Expressions
|
3
|
+
#
|
4
|
+
# Ruby port of Perl's Getopt::Declare, version 1.21,
|
5
|
+
# released May 21, 1999.
|
6
|
+
#
|
7
|
+
# $Release Version: 1.26 $
|
8
|
+
# $Date: 2007/01/15 10:53:09 $
|
9
|
+
# by Gonzalo Garramu�o
|
10
|
+
#
|
11
|
+
# For detailed instructions, see Declare.rdoc file
|
12
|
+
#
|
13
|
+
# Ruby Port:
|
14
|
+
# Copyright (c) 2004, Gonzalo Garramuno. All Rights Reserved.
|
15
|
+
# This package is free software. It may be used, redistributed
|
16
|
+
# and/or modified under the terms of the Perl Artistic License
|
17
|
+
# (see http://www.perl.com/perl/misc/Artistic.html)
|
18
|
+
#
|
19
|
+
# Original Perl Implementation:
|
20
|
+
# Damian Conway (damian@conway.org)
|
21
|
+
# Copyright (c) 1997-2000, Damian Conway. All Rights Reserved.
|
22
|
+
# This package is free software. It may be used, redistributed
|
23
|
+
# and/or modified under the terms of the Perl Artistic License
|
24
|
+
# (see http://www.perl.com/perl/misc/Artistic.html)
|
25
|
+
#
|
26
|
+
|
27
|
+
require "Getopt/DelimScanner"
|
28
|
+
|
29
|
+
|
30
|
+
# Verifies that code is valid Ruby code. returns false if not
|
31
|
+
def valid_syntax?(code, fname = 'parser_code')
|
32
|
+
eval("BEGIN {return true}\n#{code}", nil, fname, 0)
|
33
|
+
rescue Exception
|
34
|
+
false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# An add-on to the String class
|
39
|
+
class String
|
40
|
+
# Expand all tabs to spaces
|
41
|
+
def expand_tabs!( tabstop = 8 )
|
42
|
+
while self.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
|
43
|
+
val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
|
44
|
+
"#$1#$2" + (" " * val)
|
45
|
+
}
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
# Return new string with all tabs set to spaces
|
51
|
+
def expand_tabs( tabstop = 8 )
|
52
|
+
h = self.dup
|
53
|
+
while h.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
|
54
|
+
val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
|
55
|
+
"#$1#$2" + (" " * val)
|
56
|
+
}
|
57
|
+
end
|
58
|
+
h
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# Regex for removing bracket directives
|
64
|
+
BracketDirectives =
|
65
|
+
/\[\s*(?:ditto|tight|strict|no\s*case|repeatable|debug|required|mutex:.*|implies:.*|excludes:.*|requires:.*|cluster:.*)\s*\]/
|
66
|
+
|
67
|
+
|
68
|
+
module Getopt
|
69
|
+
|
70
|
+
# Main Class
|
71
|
+
class Declare
|
72
|
+
|
73
|
+
VERSION = '1.26'
|
74
|
+
|
75
|
+
# For debugging, use [debug] and it will output the ruby code as .CODE.rb
|
76
|
+
@@debug = false
|
77
|
+
|
78
|
+
# Main separator used to distinguish arguments in Getopt/Declare spec.
|
79
|
+
# By default, one or more tabs or 3 spaces or more.
|
80
|
+
@@separator = '(?:\t+| {3})'
|
81
|
+
|
82
|
+
# Class used to handle the beginning of options
|
83
|
+
class StartOpt
|
84
|
+
|
85
|
+
# Returns regex used matching start of options
|
86
|
+
def matcher(g)
|
87
|
+
'(?:()'
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns code used
|
91
|
+
def code(*t)
|
92
|
+
''
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns how to cache code in class
|
96
|
+
def cachecode(a,b)
|
97
|
+
''
|
98
|
+
end
|
99
|
+
|
100
|
+
# Helps build regex that matches parameters of flags
|
101
|
+
def trailer
|
102
|
+
nil
|
103
|
+
end
|
104
|
+
|
105
|
+
# Helps build regex that matches parameters of flags
|
106
|
+
def ows(g)
|
107
|
+
g
|
108
|
+
end
|
109
|
+
end # StartOpt
|
110
|
+
|
111
|
+
|
112
|
+
# Class used to handle the ending of options
|
113
|
+
class EndOpt < StartOpt
|
114
|
+
# Returns regex used matching end of options
|
115
|
+
def matcher(g)
|
116
|
+
'())?'
|
117
|
+
end
|
118
|
+
end # EndOpt
|
119
|
+
|
120
|
+
|
121
|
+
# Class used to handle scalar (ie.non-array) parameters
|
122
|
+
class ScalarArg
|
123
|
+
|
124
|
+
@@stdtype = {}
|
125
|
+
|
126
|
+
# (re)set standard types
|
127
|
+
def ScalarArg._reset_stdtype
|
128
|
+
@@stdtype = {
|
129
|
+
':i' => { 'pattern' => '(?:(?:%T[+-]?)%D+)' },
|
130
|
+
':n' => { 'pattern' => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
|
131
|
+
'(?:%T[eE](?:[+-])?%D+)?|%T\.%D+(?:%T[eE](?:[+-])?%D+)?))',
|
132
|
+
},
|
133
|
+
':s' => { 'pattern' => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
|
134
|
+
':qs' => { 'pattern' => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
|
135
|
+
':id' => { 'pattern' => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
|
136
|
+
':d' => { 'pattern' => '(?:%T(?:\S|\0))+',
|
137
|
+
'action' => %q%
|
138
|
+
reject( (_VAL_.nil? || !test(?d, _VAL_) ),
|
139
|
+
"in parameter '#{_PARAM_}' (\"#{_VAL_}\" is not a directory)")%
|
140
|
+
},
|
141
|
+
':if' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
|
142
|
+
'action' => %q%
|
143
|
+
reject( (_VAL_.nil? || _VAL_ != "-" && !test(?r, _VAL_) ),
|
144
|
+
"in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not readable)")%
|
145
|
+
},
|
146
|
+
':of' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
|
147
|
+
'action' => %q%
|
148
|
+
reject( (_VAL_.nil? || _VAL_ != "-" &&
|
149
|
+
test(?r, _VAL_) && !test(?w, _VAL_)),
|
150
|
+
"in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not writable)")%
|
151
|
+
},
|
152
|
+
'' => { 'pattern' => ':s', 'ind' => 1 },
|
153
|
+
|
154
|
+
':+i' => { 'pattern' => ':i',
|
155
|
+
'action' => %q%reject( _VAL_ <= 0,
|
156
|
+
"in parameter '#{_PARAM_}' (#{_VAL_} must be an integer greater than zero)")%,
|
157
|
+
'ind' => 1
|
158
|
+
},
|
159
|
+
|
160
|
+
':+n' => { 'pattern' => ':n',
|
161
|
+
'action' => %q%reject( _VAL_ <= 0.0,
|
162
|
+
"in parameter '#{_PARAM_}' (#{_VAL_} must be a number greater than zero)")%,
|
163
|
+
'ind' => 1
|
164
|
+
},
|
165
|
+
|
166
|
+
':0+i' => { 'pattern' => ':i',
|
167
|
+
'action' => %q%reject( _VAL_ < 0,
|
168
|
+
"in parameter '#{_PARAM_}' (#{_VAL_} must be an positive integer)")%,
|
169
|
+
'ind' => 1
|
170
|
+
},
|
171
|
+
|
172
|
+
':0+n' => {
|
173
|
+
'pattern' => ':n',
|
174
|
+
'action' => %q%reject( _VAL_ < 0,
|
175
|
+
"in parameter '#{_PARAM_}' (#{_VAL_} must be an positive number)")%,
|
176
|
+
'ind' => 1
|
177
|
+
},
|
178
|
+
}
|
179
|
+
|
180
|
+
end # _reset_stdtype
|
181
|
+
|
182
|
+
|
183
|
+
# Given a standard type name, return the corresponding regex
|
184
|
+
# pattern or nil
|
185
|
+
def ScalarArg.stdtype(name)
|
186
|
+
seen = {}
|
187
|
+
while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
|
188
|
+
seen[name] = 1; name = @@stdtype[name]['pattern']
|
189
|
+
end
|
190
|
+
|
191
|
+
return nil if seen[name] || !@@stdtype[name]
|
192
|
+
@@stdtype[name]['pattern']
|
193
|
+
end
|
194
|
+
|
195
|
+
def stdtype(name)
|
196
|
+
ScalarArg.stdtype(name)
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
# Given the name of a type, return its corresponding action(s)
|
201
|
+
def ScalarArg.stdactions(name)
|
202
|
+
seen = {}
|
203
|
+
actions = []
|
204
|
+
while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
|
205
|
+
seen[name] = 1
|
206
|
+
if @@stdtype[name]['action']
|
207
|
+
actions.push( @@stdtype[name]['action'] )
|
208
|
+
end
|
209
|
+
name = @@stdtype[name]['pattern']
|
210
|
+
end
|
211
|
+
|
212
|
+
if @@stdtype[name] && @@stdtype[name]['action']
|
213
|
+
actions.push( @@stdtype[name]['action'] )
|
214
|
+
end
|
215
|
+
|
216
|
+
return actions
|
217
|
+
end
|
218
|
+
|
219
|
+
# Add a new (user defined) type to the standard types
|
220
|
+
def ScalarArg.addtype(abbrev, pattern, action, ref)
|
221
|
+
|
222
|
+
typeid = ":#{abbrev}"
|
223
|
+
unless (pattern =~ /\S/)
|
224
|
+
pattern = ":s"
|
225
|
+
ref = 1
|
226
|
+
end
|
227
|
+
|
228
|
+
@@stdtype[typeid] = {}
|
229
|
+
@@stdtype[typeid]['pattern'] = "(?:#{pattern})" if pattern && !ref
|
230
|
+
@@stdtype[typeid]['pattern'] = ":#{pattern}" if pattern && ref
|
231
|
+
@@stdtype[typeid]['action'] = action if action
|
232
|
+
@@stdtype[typeid]['ind'] = ref
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
attr :name
|
237
|
+
attr :type
|
238
|
+
attr :nows
|
239
|
+
|
240
|
+
|
241
|
+
# Constructor
|
242
|
+
def initialize(name, type, nows)
|
243
|
+
@name = name
|
244
|
+
@type = type
|
245
|
+
@nows = nows
|
246
|
+
end
|
247
|
+
|
248
|
+
# Create regexp to match parameter
|
249
|
+
def matcher(g)
|
250
|
+
trailing = g ? '(?!'+Regexp::quote(g)+')' : ''
|
251
|
+
|
252
|
+
# Find type in list of standard (and user) types
|
253
|
+
stdtype = stdtype(@type)
|
254
|
+
|
255
|
+
# Handle stdtypes that are specified as regex in parameter
|
256
|
+
if (!stdtype && @type =~ %r"\A:/([^/]+)/\Z" )
|
257
|
+
stdtype = "#$1"
|
258
|
+
end
|
259
|
+
|
260
|
+
if stdtype.nil?
|
261
|
+
raise "Error: bad type in Getopt::Declare parameter variable specification near '<#{@name}#{@type}>'\nValid types are:\n" + @@stdtype.keys.inspect
|
262
|
+
end
|
263
|
+
|
264
|
+
stdtype = stdtype.dup # make a copy, as we'll change it in place
|
265
|
+
stdtype.gsub!(/\%D/,"(?:#{trailing}\\d)")
|
266
|
+
stdtype.gsub!(/\%T/,trailing)
|
267
|
+
unless ( stdtype.sub!("\%F","") )
|
268
|
+
stdtype = Getopt::Declare::Arg::negflagpat + stdtype
|
269
|
+
end
|
270
|
+
return "(?:#{stdtype})"
|
271
|
+
end
|
272
|
+
|
273
|
+
# Return string with code to process parameter
|
274
|
+
def code(*t)
|
275
|
+
if t[0]
|
276
|
+
pos1 = t[0].to_s
|
277
|
+
else
|
278
|
+
pos1 = '0'
|
279
|
+
end
|
280
|
+
|
281
|
+
c = conversion
|
282
|
+
c = "\n _VAL_ = _VAL_#{c} if _VAL_" if c
|
283
|
+
|
284
|
+
code = <<-EOS
|
285
|
+
_VAR_ = %q|<#{@name}>|
|
286
|
+
_VAL_ = @@m[#{pos1}]
|
287
|
+
_VAL_.tr!("\\0"," ") if _VAL_#{c}
|
288
|
+
EOS
|
289
|
+
|
290
|
+
actions = Getopt::Declare::ScalarArg::stdactions(@type)
|
291
|
+
|
292
|
+
for i in actions
|
293
|
+
next if i.nil?
|
294
|
+
# i.sub!(/(\s*\{)/, '\1 module '+t[1])
|
295
|
+
code << "
|
296
|
+
begin
|
297
|
+
#{i}
|
298
|
+
end
|
299
|
+
"
|
300
|
+
end
|
301
|
+
|
302
|
+
code << " #{@name} = _VAL_\n"
|
303
|
+
end
|
304
|
+
|
305
|
+
# Based on parameter type, default conversion to apply
|
306
|
+
def conversion
|
307
|
+
pat = @@stdtype[@type] ? @@stdtype[@type]['pattern'] : ''
|
308
|
+
[ @type, pat ].each { |t|
|
309
|
+
case t
|
310
|
+
when /^\:0?(\+)?i$/
|
311
|
+
return '.to_i'
|
312
|
+
when /^\:0?(\+)?n$/
|
313
|
+
return '.to_f'
|
314
|
+
end
|
315
|
+
}
|
316
|
+
return nil
|
317
|
+
end
|
318
|
+
|
319
|
+
# Return string with code to cache argument in Getopt::Declare's cache
|
320
|
+
def cachecode(ownerflag, itemcount)
|
321
|
+
if itemcount > 1
|
322
|
+
" @cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n"
|
323
|
+
else
|
324
|
+
" @cache['#{ownerflag}'] = #{@name}\n"
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
# Helps build regex that matches parameters of flags
|
329
|
+
def trailer
|
330
|
+
nil # MEANS TRAILING PARAMETER VARIABLE (in Perl,was '')
|
331
|
+
end
|
332
|
+
|
333
|
+
# Helps build regex that matches parameters of flags
|
334
|
+
# Wraps parameter passed for #$1, etc. matching
|
335
|
+
def ows(g)
|
336
|
+
return '[\s|\0]*(' + g + ')' unless @nows
|
337
|
+
'('+ g +')'
|
338
|
+
end
|
339
|
+
|
340
|
+
end # ScalarArg
|
341
|
+
|
342
|
+
|
343
|
+
# Class used to handle array arguments
|
344
|
+
class ArrayArg < ScalarArg
|
345
|
+
|
346
|
+
# Create regexp to match array
|
347
|
+
def matcher(g)
|
348
|
+
suffix = !g.nil? ? '([\s\0]+)' : ''
|
349
|
+
scalar = super # contains regex to match a scalar element
|
350
|
+
# we match one obligatory element, and one or more optionals ')*'
|
351
|
+
return scalar + '(?:[\s\0]+' + scalar + ')*' + suffix
|
352
|
+
end
|
353
|
+
|
354
|
+
# Return string with code to process array parameter
|
355
|
+
def code(*t)
|
356
|
+
|
357
|
+
if t[0]
|
358
|
+
pos1 = t[0].to_s
|
359
|
+
else
|
360
|
+
pos1 = '0'
|
361
|
+
end
|
362
|
+
|
363
|
+
code = <<-EOS
|
364
|
+
_VAR_ = %q|<#{@name}>|
|
365
|
+
_VAL_ = nil
|
366
|
+
#{@name} = (@@m[#{pos1}]||'').split(' ').map { |i|
|
367
|
+
i.tr("\\0", " ") }
|
368
|
+
EOS
|
369
|
+
|
370
|
+
# Handle conversion to proper type
|
371
|
+
c = conversion
|
372
|
+
if c
|
373
|
+
code << " #{@name}.map! { |i| i#{c} }\n"
|
374
|
+
end
|
375
|
+
|
376
|
+
actions = Getopt::Declare::ScalarArg::stdactions(@type)
|
377
|
+
if actions.size > 0
|
378
|
+
code << " for _VAL_ in #{@name}\n"
|
379
|
+
for i in actions
|
380
|
+
code << " #{i}\n"
|
381
|
+
end
|
382
|
+
code << " end\n\n"
|
383
|
+
end
|
384
|
+
return code
|
385
|
+
end
|
386
|
+
|
387
|
+
# Return string with code to cache array in Getopt::Declare's cache
|
388
|
+
def cachecode(ownerflag, itemcount)
|
389
|
+
if itemcount > 1
|
390
|
+
" @cache['#{ownerflag}']['<#{@name}>'] = [] unless @cache['#{ownerflag}']['<#{@name}>']
|
391
|
+
@cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n"
|
392
|
+
else
|
393
|
+
" @cache['#{ownerflag}'] = #{@name}\n"
|
394
|
+
end
|
395
|
+
end
|
396
|
+
end # ArrayArg
|
397
|
+
|
398
|
+
|
399
|
+
# Class used to handle punctuations (., -, etc.)
|
400
|
+
class Punctuator
|
401
|
+
|
402
|
+
# Constructor
|
403
|
+
def initialize(text, nows)
|
404
|
+
@text = text
|
405
|
+
@nows = nows
|
406
|
+
end
|
407
|
+
|
408
|
+
# Return regex that matches this punctuation
|
409
|
+
def matcher(g)
|
410
|
+
Arg::negflagpat + Regexp::quote(@text)
|
411
|
+
end
|
412
|
+
|
413
|
+
# Return string with code to process punctuation
|
414
|
+
def code(*t)
|
415
|
+
|
416
|
+
if t[0]
|
417
|
+
pos1 = t[0].to_s
|
418
|
+
else
|
419
|
+
pos1 = '0'
|
420
|
+
end
|
421
|
+
" if @@m[#{pos1}] && !@@m[#{pos1}].empty?
|
422
|
+
_PUNCT_['#{@text}'] = @@m[#{pos1}]
|
423
|
+
end
|
424
|
+
"
|
425
|
+
end
|
426
|
+
|
427
|
+
# Return string with code to cache punctuation in Getopt::Declare's cache
|
428
|
+
def cachecode(ownerflag, itemcount)
|
429
|
+
if itemcount > 1
|
430
|
+
" @cache['#{ownerflag}']['#{@text}'] = _PUNCT_['#{@text}']\n"
|
431
|
+
else
|
432
|
+
" unless @cache['#{ownerflag}']\n" +
|
433
|
+
" @cache['#{ownerflag}'] = _PUNCT_['#{@text}'] || 1\n" +
|
434
|
+
" end\n"
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
# Helps build regex that matches parameters of flags
|
439
|
+
def trailer
|
440
|
+
@text
|
441
|
+
end
|
442
|
+
|
443
|
+
# Helps build regex that matches parameters of flags
|
444
|
+
# Wraps parameter passed for #$1, etc. matching
|
445
|
+
def ows(g)
|
446
|
+
return '[\s\0]*(' + g + ')' unless @nows
|
447
|
+
'(' + g + ')'
|
448
|
+
end #ows
|
449
|
+
|
450
|
+
end # Punctuator
|
451
|
+
|
452
|
+
|
453
|
+
# Class used to handle other arguments (flags, etc)
|
454
|
+
class Arg
|
455
|
+
|
456
|
+
@@nextid = 0
|
457
|
+
|
458
|
+
|
459
|
+
Helpcmd = %w( -help --help -Help --Help -HELP --HELP -h -H )
|
460
|
+
|
461
|
+
@@helpcmdH = {}
|
462
|
+
for i in Helpcmd; @@helpcmdH[i] = 1; end
|
463
|
+
|
464
|
+
def Arg.besthelp
|
465
|
+
for i in Helpcmd; return i if @@helpcmdH[i]; end
|
466
|
+
end
|
467
|
+
|
468
|
+
# Create regex of help flags based on help shortcuts left
|
469
|
+
def Arg.helppat
|
470
|
+
@@helpcmdH.keys.join('|')
|
471
|
+
end
|
472
|
+
|
473
|
+
|
474
|
+
Versioncmd = %w( -version --version -Version --Version
|
475
|
+
-VERSION --VERSION -v -V )
|
476
|
+
@@versioncmdH = {}
|
477
|
+
for i in Versioncmd; @@versioncmdH[i] = 1; end
|
478
|
+
|
479
|
+
def Arg.bestversion
|
480
|
+
for i in Versioncmd; return i if @@versioncmdH[i]; end
|
481
|
+
end
|
482
|
+
|
483
|
+
# Create regex of version flags based on help shortcuts left
|
484
|
+
def Arg.versionpat
|
485
|
+
@@versioncmdH.keys.join('|')
|
486
|
+
end
|
487
|
+
|
488
|
+
@@flags = []
|
489
|
+
@@posflagpat = nil
|
490
|
+
@@negflagpat = nil
|
491
|
+
|
492
|
+
def Arg.clear
|
493
|
+
@@flags = []
|
494
|
+
@@nextid = 0
|
495
|
+
@@posflagpat = nil
|
496
|
+
@@negflagpath = nil
|
497
|
+
end
|
498
|
+
|
499
|
+
# Return string with regex that avoids all flags in declaration
|
500
|
+
def Arg.negflagpat(*t)
|
501
|
+
if !@@negflagpat && @@flags
|
502
|
+
@@negflagpat = ( @@flags.map { |i|
|
503
|
+
"(?!" + Regexp::quote(i) + ")" } ).join('')
|
504
|
+
else
|
505
|
+
@@negflagpat
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
# Return string with regex that matches any of the flags in declaration
|
510
|
+
def Arg.posflagpat(*t)
|
511
|
+
if !@@posflagpat && @@flags
|
512
|
+
@@posflagpat = '(?:' + ( @@flags.map { |i|
|
513
|
+
Regexp::quote(i) } ).join('|') + ')'
|
514
|
+
else
|
515
|
+
@@posflagpat
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
attr_accessor :flag, :args, :actions, :ditto, :nocase
|
520
|
+
attr_accessor :required, :id, :repeatable, :desc
|
521
|
+
attr_writer :requires
|
522
|
+
|
523
|
+
|
524
|
+
#
|
525
|
+
def _enfound(original)
|
526
|
+
expr = original.gsub(/((?:&&|\|\|)?\s*(?:[!(]\s*)*)([^ \t\n|&\)]+)/x,
|
527
|
+
'\1_FOUND_[\'\2\']')
|
528
|
+
|
529
|
+
if !valid_syntax?( expr )
|
530
|
+
raise "Error: bad condition in [requires: #{original}]\n"
|
531
|
+
end
|
532
|
+
expr
|
533
|
+
end
|
534
|
+
|
535
|
+
def requires
|
536
|
+
return nil unless @requires
|
537
|
+
_enfound(@requires)
|
538
|
+
end
|
539
|
+
|
540
|
+
# Constructor
|
541
|
+
def initialize(spec, desc, dittoflag)
|
542
|
+
first = 1
|
543
|
+
|
544
|
+
|
545
|
+
@@nextid += 1
|
546
|
+
@flag = ''
|
547
|
+
@foundid = nil
|
548
|
+
@args = []
|
549
|
+
@actions = []
|
550
|
+
@ditto = dittoflag
|
551
|
+
@required = false
|
552
|
+
@requires = nil
|
553
|
+
@id = @@nextid
|
554
|
+
@desc = spec.dup
|
555
|
+
@items = 0
|
556
|
+
@nocase = false
|
557
|
+
|
558
|
+
@desc.sub!(/\A\s*(.*?)\s*\Z/,'\1')
|
559
|
+
|
560
|
+
while spec && spec != ''
|
561
|
+
begin
|
562
|
+
|
563
|
+
# OPTIONAL
|
564
|
+
if spec.sub!( /\A(\s*)\[/, '\1' )
|
565
|
+
@args.push( StartOpt.new )
|
566
|
+
next
|
567
|
+
elsif spec.sub!(/\A\s*\]/,"")
|
568
|
+
@args.push( EndOpt.new )
|
569
|
+
next
|
570
|
+
end
|
571
|
+
|
572
|
+
# ARG
|
573
|
+
|
574
|
+
se = DelimScanner::new( spec )
|
575
|
+
tmp = se.scanBracketed('<>')
|
576
|
+
|
577
|
+
arg = nows = nil
|
578
|
+
arg, spec, nows = tmp[:match], tmp[:suffix], tmp[:prefix] if tmp
|
579
|
+
|
580
|
+
|
581
|
+
if arg
|
582
|
+
arg =~ /\A(\s*)(<)([a-zA-Z]\w*)(:[^>]+|)>/ or
|
583
|
+
raise "Error: bad Getopt::Declare parameter variable specification near '#{arg}'\n"
|
584
|
+
|
585
|
+
# NAME,TYPE,NOW
|
586
|
+
details = [ "#$3", "#$4", !first && !(nows.length>0) ]
|
587
|
+
|
588
|
+
if spec && spec.sub!( /\A\.\.\./, "") # ARRAY ARG
|
589
|
+
@args.push( ArrayArg.new(*details) )
|
590
|
+
else # SCALAR ARG
|
591
|
+
@args.push( ScalarArg.new(*details) )
|
592
|
+
end
|
593
|
+
@items += 1
|
594
|
+
next
|
595
|
+
|
596
|
+
# PUNCTUATION
|
597
|
+
elsif spec.sub!( /\A(\s*)((\\.|[^\] \t\n\[<])+)/, '' )
|
598
|
+
ows, punct = $1, $2
|
599
|
+
punct.gsub!( /\\(?!\\)(.)/, '\1' )
|
600
|
+
|
601
|
+
if first
|
602
|
+
spec =~ /\A(\S+)/
|
603
|
+
@foundid = "#{punct}#{$1}"
|
604
|
+
@flag = punct
|
605
|
+
@@flags.push( punct )
|
606
|
+
else
|
607
|
+
@args.push( Punctuator.new(punct, !(ows.size > 0)) )
|
608
|
+
@items += 1
|
609
|
+
end
|
610
|
+
|
611
|
+
else
|
612
|
+
break
|
613
|
+
end # if arg/spec.sub
|
614
|
+
ensure
|
615
|
+
first = nil
|
616
|
+
end
|
617
|
+
end # while
|
618
|
+
|
619
|
+
|
620
|
+
@@helpcmdH.delete(@flag) if @@helpcmdH.key?(@flag)
|
621
|
+
@@versioncmdH.delete(@flag) if @@versioncmdH.key?(@flag)
|
622
|
+
end # initialize
|
623
|
+
|
624
|
+
|
625
|
+
|
626
|
+
# Return String with code to parse this argument (ie. flag)
|
627
|
+
def code(*t)
|
628
|
+
owner = t[0]
|
629
|
+
mod = t[1]
|
630
|
+
|
631
|
+
|
632
|
+
code = "\n"
|
633
|
+
flag = @flag
|
634
|
+
clump = owner.clump
|
635
|
+
i = 0
|
636
|
+
nocasei = ((Getopt::Declare::nocase || @nocase) ? 'i' : '')
|
637
|
+
|
638
|
+
code << " catch(:paramout) do\n"
|
639
|
+
code += !@repeatable? " while !_FOUND_['" + self.foundid + "']" :
|
640
|
+
" while true"
|
641
|
+
if (flag && (clump==1 && flag !~ /\A[^a-z0-9]+[a-z0-9]\Z/i ||
|
642
|
+
(clump<3 && @args.size > 0 )))
|
643
|
+
code << ' && !_lastprefix'
|
644
|
+
end
|
645
|
+
|
646
|
+
code << '
|
647
|
+
begin
|
648
|
+
catch(:param) do
|
649
|
+
_pos = _nextpos if _args
|
650
|
+
_PUNCT_ = {}
|
651
|
+
'
|
652
|
+
|
653
|
+
if flag != ''
|
654
|
+
# This boundary is to handle -- option, so that if user uses
|
655
|
+
# --foo and --foo is not a flag, it does not become
|
656
|
+
# -- and unused: 'foo', but an error saying flag '--foo' not defined.
|
657
|
+
boundary = ''
|
658
|
+
boundary = '(\s+|\Z)' if flag =~ /^(--|-|\+|\+\+)$/
|
659
|
+
|
660
|
+
code << '
|
661
|
+
_args && _pos = gindex( _args, /\G[\s|\0]*' +
|
662
|
+
Regexp::quote(flag) + boundary + '/' + nocasei + ", _pos) or throw(:paramout)
|
663
|
+
unless @_errormsg
|
664
|
+
@_errormsg = %q|incorrect specification of '" + flag + "' parameter|
|
665
|
+
end
|
666
|
+
"
|
667
|
+
elsif ( ScalarArg::stdtype(@args[0].type)||'') !~ /\%F/
|
668
|
+
code << "\n throw(:paramout) if @_errormsg\n"
|
669
|
+
end
|
670
|
+
|
671
|
+
|
672
|
+
code << "\n _PARAM_ = '" + self.name + "'\n"
|
673
|
+
|
674
|
+
|
675
|
+
trailer = []
|
676
|
+
i = @args.size-1
|
677
|
+
while i > 0
|
678
|
+
trailer[i-1] = @args[i].trailer
|
679
|
+
trailer[i-1] = trailer[i] unless trailer[i-1]
|
680
|
+
i -= 1
|
681
|
+
end # while i
|
682
|
+
|
683
|
+
if @args
|
684
|
+
code << "\n"+' _args && _pos = gindex( _args, /\G'
|
685
|
+
|
686
|
+
@args.each_with_index { |arg, i|
|
687
|
+
code << arg.ows(arg.matcher(trailer[i]))
|
688
|
+
}
|
689
|
+
|
690
|
+
code << '/x' + nocasei + ", _pos ) or throw(:paramout)\n"
|
691
|
+
end # if @args
|
692
|
+
|
693
|
+
@args.each_with_index { |arg, i|
|
694
|
+
code << arg.code(i,mod) #, $flag ????
|
695
|
+
}
|
696
|
+
|
697
|
+
if flag
|
698
|
+
mutexlist = owner.mutex[flag] ?
|
699
|
+
( owner.mutex[flag].map {|i| "'#{i}'"} ).join(',') : ''
|
700
|
+
|
701
|
+
code << "
|
702
|
+
if _invalid.has_key?('#{flag}')
|
703
|
+
@_errormsg = %q|parameter '#{flag}' not allowed with parameter '| + _invalid['#{flag}'] + %q|'|
|
704
|
+
throw(:paramout)
|
705
|
+
else
|
706
|
+
for i in [#{mutexlist}]
|
707
|
+
_invalid[i] = '#{flag}'
|
708
|
+
end
|
709
|
+
end #if/then
|
710
|
+
|
711
|
+
"
|
712
|
+
end
|
713
|
+
|
714
|
+
|
715
|
+
|
716
|
+
for action in @actions
|
717
|
+
#action.sub!( /(\s*\{)/, '\1 module '+mod ) # @TODO
|
718
|
+
code << "\n " + action + "\n"
|
719
|
+
end
|
720
|
+
|
721
|
+
if flag && @items==0
|
722
|
+
code << "\n @cache['#{flag}'] = '#{flag}'\n"
|
723
|
+
if @ditto
|
724
|
+
code << "\n @cache['#{@ditto.flag}'] = '#{flag}'\n"
|
725
|
+
end
|
726
|
+
end
|
727
|
+
|
728
|
+
if @items > 1
|
729
|
+
code << " @cache['#{self.name}'] = {} unless @cache['#{self.name}'].kind_of?(Hash)\n"
|
730
|
+
if @ditto
|
731
|
+
code << "\n @cache['#{@ditto.name}'] = {} unless @cache['#{@ditto.name}'].kind_of?(Hash)\n"
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
for subarg in @args
|
736
|
+
code << subarg.cachecode(self.name,@items)
|
737
|
+
if ditto
|
738
|
+
code << subarg.cachecode(@ditto.name,@items)
|
739
|
+
end
|
740
|
+
end
|
741
|
+
|
742
|
+
if flag =~ /\A([^a-z0-9]+)/i
|
743
|
+
code << ' _lastprefix = "'+ Regexp::quote("#$1") + '"' + "\n"
|
744
|
+
else
|
745
|
+
code << " _lastprefix = nil\n"
|
746
|
+
end
|
747
|
+
|
748
|
+
code << "
|
749
|
+
_FOUND_['"+ self.foundid + "'] = 1
|
750
|
+
throw :arg if _pos > 0
|
751
|
+
_nextpos = _args.size
|
752
|
+
throw :alldone
|
753
|
+
end # catch(:param)
|
754
|
+
end # begin
|
755
|
+
end # while
|
756
|
+
end # catch(:paramout)
|
757
|
+
"
|
758
|
+
|
759
|
+
code
|
760
|
+
end
|
761
|
+
|
762
|
+
# Return name of argument, which can be flag's name or variable's name
|
763
|
+
def name
|
764
|
+
return @flag unless @flag.empty?
|
765
|
+
for i in @args
|
766
|
+
return "<#{i.name}>" if i.respond_to?(:name)
|
767
|
+
end
|
768
|
+
raise "Unknown flag name for parameter #{self.desc}"
|
769
|
+
end
|
770
|
+
|
771
|
+
# Return foundid of argument, which can be flag's name or variable's name
|
772
|
+
def foundid
|
773
|
+
return @foundid || self.name
|
774
|
+
end
|
775
|
+
|
776
|
+
end # Arg
|
777
|
+
|
778
|
+
|
779
|
+
private
|
780
|
+
|
781
|
+
class << self
|
782
|
+
@nocase = false
|
783
|
+
attr_accessor :nocase
|
784
|
+
end
|
785
|
+
|
786
|
+
#
|
787
|
+
# This is an additional function added to the class to simulate Perl's
|
788
|
+
# pos() \G behavior and m///g
|
789
|
+
#
|
790
|
+
# It performs a regex match, and returns the last index position of the
|
791
|
+
# match or nil. On successive invocations, it allows doing regex matches
|
792
|
+
# NOT from the beginning of the string easily.
|
793
|
+
#
|
794
|
+
# Class Array @@m stores the list of matches, as #$1 and similar
|
795
|
+
# variables have short lifespan in ruby, unlike perl.
|
796
|
+
#
|
797
|
+
def gindex(str, re, pos)
|
798
|
+
@@m.clear()
|
799
|
+
if pos = str.index( re, pos )
|
800
|
+
l = $&.size # length of match
|
801
|
+
if l > 0
|
802
|
+
@@m[0] = "#$1"
|
803
|
+
@@m[1] = "#$2"
|
804
|
+
@@m[2] = "#$3"
|
805
|
+
@@m[3] = "#$4"
|
806
|
+
@@m[4] = "#$5"
|
807
|
+
@@m[5] = "#$6"
|
808
|
+
@@m[6] = "#$7"
|
809
|
+
@@m[7] = "#$8"
|
810
|
+
@@m[8] = "#$9"
|
811
|
+
pos += l
|
812
|
+
end
|
813
|
+
end
|
814
|
+
pos
|
815
|
+
end
|
816
|
+
|
817
|
+
# Given an array or hash, flatten them to a string
|
818
|
+
def flatten(val, nested = nil)
|
819
|
+
case val
|
820
|
+
when Array
|
821
|
+
return val.map{ |i| flatten(i,1) }.join(" ")
|
822
|
+
when Hash
|
823
|
+
return val.keys.map{ |i| nested ||
|
824
|
+
i =~ /^-/ ? [i, flatten(val[i],1)] :
|
825
|
+
[flatten(val[i],1)] }.join(" ")
|
826
|
+
else
|
827
|
+
return val
|
828
|
+
end
|
829
|
+
end
|
830
|
+
|
831
|
+
# Read the next line from stdin
|
832
|
+
def _get_nextline
|
833
|
+
$stdin.readline
|
834
|
+
end
|
835
|
+
|
836
|
+
# For each file provided and found, read it in
|
837
|
+
def _load_sources( _get_nextline, files )
|
838
|
+
text = ''
|
839
|
+
found = []
|
840
|
+
|
841
|
+
for i in files
|
842
|
+
begin
|
843
|
+
f = File.open(i,"r")
|
844
|
+
rescue
|
845
|
+
next
|
846
|
+
end
|
847
|
+
|
848
|
+
if f.tty?
|
849
|
+
found.push( '<STDIN>' )
|
850
|
+
_get_nextline = method(:_get_nextline)
|
851
|
+
else
|
852
|
+
found.push( i );
|
853
|
+
t = f.readlines.join(' ')
|
854
|
+
t.tr!('\t\n',' ')
|
855
|
+
text += t
|
856
|
+
end
|
857
|
+
end
|
858
|
+
|
859
|
+
return nil unless found.size > 0
|
860
|
+
text = $stdin.readline if text.empty?
|
861
|
+
return [text, found.join(' or ')]
|
862
|
+
end
|
863
|
+
|
864
|
+
|
865
|
+
# Check parameter description for special options
|
866
|
+
def _infer(desc, arg, mutex)
|
867
|
+
while desc.sub!(/\[\s*mutex:\s*(.*?)\]/i,"")
|
868
|
+
_mutex(mutex, "#$1".split(' '))
|
869
|
+
end
|
870
|
+
|
871
|
+
if desc =~ /\[\s*no\s*case\s*\]/i
|
872
|
+
if arg
|
873
|
+
arg.nocase = true
|
874
|
+
else
|
875
|
+
nocase = true
|
876
|
+
end
|
877
|
+
end
|
878
|
+
|
879
|
+
if !arg.nil?
|
880
|
+
if desc =~ /.*\[\s*excludes:\s*(.*?)\]/i
|
881
|
+
_exclude(mutex, arg.name, ("#$1".split(' ')))
|
882
|
+
end
|
883
|
+
|
884
|
+
if desc =~ /.*\[\s*requires:\s*(.*?)\]/i
|
885
|
+
arg.requires = "#$1"
|
886
|
+
end
|
887
|
+
|
888
|
+
arg.required = ( desc =~ /\[\s*required\s*\]/i )
|
889
|
+
|
890
|
+
arg.repeatable = ( desc =~ /\[\s*repeatable\s*\]/i )
|
891
|
+
end
|
892
|
+
|
893
|
+
_typedef(desc) while desc.sub!(/.*?\[\s*pvtype:\s*/,"")
|
894
|
+
|
895
|
+
end
|
896
|
+
|
897
|
+
|
898
|
+
|
899
|
+
# Extract a new type from the description and add it to the list
|
900
|
+
# of standard types
|
901
|
+
def _typedef(desc)
|
902
|
+
se = DelimScanner::new( desc )
|
903
|
+
tmp = se.scanQuotelike
|
904
|
+
|
905
|
+
name = nil
|
906
|
+
name, desc = tmp[:delimText], tmp[:suffix] if tmp
|
907
|
+
|
908
|
+
unless name
|
909
|
+
desc.sub!(/\A\s*([^\] \t\n]+)/,"") and name = "#$1"
|
910
|
+
end
|
911
|
+
|
912
|
+
raise "Error: bad type directive (missing type name): [pvtype: " +
|
913
|
+
desc[0,desc.index(']')||20] + "....\n" unless name
|
914
|
+
|
915
|
+
se = DelimScanner::new( desc )
|
916
|
+
tmp = se.scanQuotelike('\s*:?\s*')
|
917
|
+
|
918
|
+
# @TODO What is element 2 of extract_quotelike? :trail is a fake here
|
919
|
+
# pat,desc,ind = (extract_quotelike(desc,'\s*:?\s*'))[5,1,2]
|
920
|
+
pat = ind = nil
|
921
|
+
pat, desc, ind = tmp[:match], tmp[:suffix], tmp[:prefix] if tmp
|
922
|
+
pat = pat[1..-2] if pat
|
923
|
+
|
924
|
+
unless pat
|
925
|
+
desc.sub!(/\A\s*(:?)\s*([^\] \t\n]+)/,"") and pat = "#$2" and ind = "#$1"
|
926
|
+
end
|
927
|
+
|
928
|
+
pat = '' unless pat
|
929
|
+
|
930
|
+
|
931
|
+
se = DelimScanner::new( desc )
|
932
|
+
action = se.extractCodeblock || ''
|
933
|
+
|
934
|
+
desc.sub!( Regexp::quote(action).to_re, '' )
|
935
|
+
action = action[1..-2]
|
936
|
+
|
937
|
+
raise "Error: bad type directive (expected closing ']' but found " +
|
938
|
+
"'#$1' instead): [pvtype: #{name} " + (pat ? "/#{pat}/" : '') +
|
939
|
+
" action:#{action} #$1#$2....\n" if desc =~ /\A\s*([^\] \t\n])(\S*)/
|
940
|
+
|
941
|
+
|
942
|
+
Getopt::Declare::ScalarArg::addtype(name,pat,action,ind=~/:/)
|
943
|
+
end
|
944
|
+
|
945
|
+
# Handle quote replacements for [ditto] flag
|
946
|
+
def _ditto(originalflag, originaldesc, extra)
|
947
|
+
if originaldesc =~ /\n.*\n/
|
948
|
+
originaldesc = "Same as #{originalflag} "
|
949
|
+
else
|
950
|
+
originaldesc.chomp
|
951
|
+
originaldesc.gsub!(/\S/,'"')
|
952
|
+
while originaldesc.gsub!(/"("+)"/,' \1 ')
|
953
|
+
end
|
954
|
+
originaldesc.gsub!(/""/,'" ')
|
955
|
+
end
|
956
|
+
|
957
|
+
"#{originaldesc}#{extra}\n"
|
958
|
+
end
|
959
|
+
|
960
|
+
# Check mutex conditions
|
961
|
+
def _mutex(mref, mutexlist)
|
962
|
+
for flag in mutexlist
|
963
|
+
mref[flag] = [] unless mref[flag]
|
964
|
+
for otherflag in mutexlist
|
965
|
+
next if flag == otherflag
|
966
|
+
mref[flag].push( otherflag )
|
967
|
+
end
|
968
|
+
end
|
969
|
+
end
|
970
|
+
|
971
|
+
# Check exclude conditions
|
972
|
+
def _exclude(mref, excluded, mutexlist)
|
973
|
+
for flag in mutexlist
|
974
|
+
unless flag == excluded
|
975
|
+
mref[flag] = [] unless mref[flag]
|
976
|
+
mref[excluded] = [] unless mref[excluded]
|
977
|
+
mref[excluded].push( flag )
|
978
|
+
mref[flag].push( excluded )
|
979
|
+
end
|
980
|
+
end
|
981
|
+
end
|
982
|
+
|
983
|
+
# Returns a regex to match a single argument line
|
984
|
+
def re_argument
|
985
|
+
/\A(.*?\S.*?#{@@separator})(.*?\n)/
|
986
|
+
end
|
987
|
+
|
988
|
+
# Returns a regex to keep matching a multi-line description
|
989
|
+
# for an argument.
|
990
|
+
def re_more_desc
|
991
|
+
/\A((?![ \t]*(\{|\n)|.*?\S.*?#{@@separator}.*?\S).*?\S.*\n)/
|
992
|
+
end
|
993
|
+
|
994
|
+
public
|
995
|
+
|
996
|
+
# Constructor
|
997
|
+
def initialize(*opts)
|
998
|
+
@cache = nil
|
999
|
+
|
1000
|
+
Getopt::Declare::Arg::clear
|
1001
|
+
|
1002
|
+
# HANDLE SHORT-CIRCUITS
|
1003
|
+
return if opts.size==2 && (!opts[1] || opts[1] == '-SKIP')
|
1004
|
+
|
1005
|
+
grammar, source = opts
|
1006
|
+
|
1007
|
+
if grammar.nil?
|
1008
|
+
raise "Error: No grammar description provided."
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
### REMOVED PREDEF GRAMMAR AS IT WAS NOT DOCUMENTED NOR
|
1012
|
+
### WORKING IN PERL'S Declare.pm VERSION.
|
1013
|
+
|
1014
|
+
# PRESERVE ESCAPED '['s
|
1015
|
+
grammar.gsub!(/\\\[/,"\255")
|
1016
|
+
|
1017
|
+
# MAKE SURE GRAMMAR ENDS WITH A NEWLINE.
|
1018
|
+
grammar.sub!(/([^\n])\Z/,'\1'+"\n")
|
1019
|
+
|
1020
|
+
@usage = grammar.dup
|
1021
|
+
|
1022
|
+
# SET-UP
|
1023
|
+
i = grammar
|
1024
|
+
_args = []
|
1025
|
+
_mutex = {}
|
1026
|
+
_strict = false
|
1027
|
+
_all_repeatable = false
|
1028
|
+
_lastdesc = nil
|
1029
|
+
arg = nil
|
1030
|
+
Getopt::Declare::nocase = false
|
1031
|
+
Getopt::Declare::ScalarArg::_reset_stdtype
|
1032
|
+
|
1033
|
+
|
1034
|
+
# CONSTRUCT GRAMMAR
|
1035
|
+
while i.length > 0
|
1036
|
+
|
1037
|
+
# COMMENT:
|
1038
|
+
i.sub!(/\A[ \t]*#.*\n/,"") and next
|
1039
|
+
|
1040
|
+
|
1041
|
+
# TYPE DIRECTIVE:
|
1042
|
+
se = DelimScanner::new( i )
|
1043
|
+
|
1044
|
+
if i =~ /\A\s*\[\s*pvtype:/
|
1045
|
+
_action = se.extractBracketed("[")
|
1046
|
+
if _action
|
1047
|
+
i.sub!( Regexp::quote( _action ).to_re, "" ) ### @GGA: added
|
1048
|
+
i.sub!(/\A[ \t]*\n/,"") ### @GGA: added
|
1049
|
+
_action.sub!(/.*?\[\s*pvtype:\s*/,"")
|
1050
|
+
_typedef(_action)
|
1051
|
+
next
|
1052
|
+
end # if
|
1053
|
+
end
|
1054
|
+
|
1055
|
+
# ACTION
|
1056
|
+
codeblockDelimiters = {
|
1057
|
+
'{' => '}',
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
_action = se.extractCodeblock(codeblockDelimiters)
|
1061
|
+
if _action
|
1062
|
+
i.sub!( Regexp::quote(_action ).to_re, "" )
|
1063
|
+
i.sub!(/\A[ \t]*\n/,"")
|
1064
|
+
_action = _action[1..-2]
|
1065
|
+
|
1066
|
+
if !valid_syntax?( _action )
|
1067
|
+
raise "Error: bad action in Getopt::Declare specification:" +
|
1068
|
+
"\n\n#{_action}\n\n\n"
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
if _args.length == 0
|
1072
|
+
raise "Error: unattached action in Getopt::Declare specification:\n#{_action}\n" +
|
1073
|
+
"\t(did you forget the tab after the preceding parameter specification?)\n"
|
1074
|
+
end
|
1075
|
+
|
1076
|
+
_args.last.actions.push( _action )
|
1077
|
+
next
|
1078
|
+
elsif i =~ /\A(\s*[{].*)/
|
1079
|
+
raise "Error: incomplete action in Getopt::Declare specification:\n$1.....\n" +
|
1080
|
+
"\t(did you forget a closing '}'?)\n"
|
1081
|
+
end
|
1082
|
+
|
1083
|
+
|
1084
|
+
# ARG + DESC:
|
1085
|
+
if i.sub!(re_argument,"")
|
1086
|
+
spec = "#$1".strip
|
1087
|
+
desc = "#$2"
|
1088
|
+
_strict ||= desc =~ /\[\s*strict\s*\]/
|
1089
|
+
|
1090
|
+
while i.sub!(re_more_desc,"")
|
1091
|
+
desc += "#$1"
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
ditto = nil
|
1095
|
+
if _lastdesc and desc.sub!(/\A\s*\[\s*ditto\s*\]/,_lastdesc)
|
1096
|
+
ditto = arg
|
1097
|
+
else
|
1098
|
+
_lastdesc = desc
|
1099
|
+
end
|
1100
|
+
|
1101
|
+
# Check for GNU spec line like: -d, --debug
|
1102
|
+
arg = nil
|
1103
|
+
if spec =~ /(-[\w_\d]+),\s+(--[\w_\d]+)(\s+.*)?/
|
1104
|
+
specs = ["#$1#$3", "#$2#$3"]
|
1105
|
+
specs.each { |spec|
|
1106
|
+
arg = Arg.new(spec,desc,ditto)
|
1107
|
+
_args.push( arg )
|
1108
|
+
_infer(desc, arg, _mutex)
|
1109
|
+
ditto = arg
|
1110
|
+
}
|
1111
|
+
else
|
1112
|
+
arg = Arg.new(spec,desc,ditto)
|
1113
|
+
_args.push( arg )
|
1114
|
+
_infer(desc, arg, _mutex)
|
1115
|
+
end
|
1116
|
+
|
1117
|
+
|
1118
|
+
next
|
1119
|
+
end
|
1120
|
+
|
1121
|
+
# OTHERWISE: DECORATION
|
1122
|
+
i.sub!(/((?:(?!\[\s*pvtype:).)*)(\n|(?=\[\s*pvtype:))/,"")
|
1123
|
+
decorator = "#$1"
|
1124
|
+
_strict ||= decorator =~ /\[\s*strict\s*\]/
|
1125
|
+
_infer(decorator, nil, _mutex)
|
1126
|
+
|
1127
|
+
_all_repeatable = true if decorator =~ /\[\s*repeatable\s*\]/
|
1128
|
+
@@debug = true if decorator =~ /\[\s*debug\s*\]/
|
1129
|
+
|
1130
|
+
end # while i.length
|
1131
|
+
|
1132
|
+
|
1133
|
+
_lastactions = nil
|
1134
|
+
for i in _args
|
1135
|
+
if _lastactions && i.ditto && i.actions.size == 0
|
1136
|
+
i.actions = _lastactions
|
1137
|
+
else
|
1138
|
+
_lastactions = i.actions
|
1139
|
+
end
|
1140
|
+
|
1141
|
+
if _all_repeatable
|
1142
|
+
i.repeatable = 1
|
1143
|
+
end
|
1144
|
+
end
|
1145
|
+
|
1146
|
+
# Sort flags based on criteria described in docs
|
1147
|
+
# Sadly, this cannot be reduced to sort_by
|
1148
|
+
_args = _args.sort() { |a,b|
|
1149
|
+
( b.flag.length() <=> a.flag.length() ) or
|
1150
|
+
( b.flag == a.flag and ( b.args.length() <=> a.args.length() ) ) or
|
1151
|
+
( a.id <=> b.id )
|
1152
|
+
}
|
1153
|
+
# _args = _args.sort_by { |a| [a.flag.size, a.flag, a.args.size, a.id] }
|
1154
|
+
|
1155
|
+
|
1156
|
+
# Handle clump
|
1157
|
+
clump = (@usage =~ /\[\s*cluster:\s*none\s*\]/i) ? 0 :
|
1158
|
+
(@usage =~ /\[\s*cluster:\s*singles?\s*\]/i) ? 1 :
|
1159
|
+
(@usage =~ /\[\s*cluster:\s*flags?\s*\]/i) ? 2 :
|
1160
|
+
(@usage =~ /\[\s*cluster:\s*any\s*\]/i) ? 3 :
|
1161
|
+
(@usage =~ /\[\s*cluster:(.*)\s*\]/i) ? "r" : 3
|
1162
|
+
raise "Error: unknown clustering mode: [cluster:#$1]\n" if clump == "r"
|
1163
|
+
|
1164
|
+
# CONSTRUCT OBJECT ITSELF
|
1165
|
+
@args = _args
|
1166
|
+
@mutex = _mutex
|
1167
|
+
@helppat = Arg::helppat()
|
1168
|
+
@verspat = Arg::versionpat()
|
1169
|
+
|
1170
|
+
@strict = _strict
|
1171
|
+
@clump = clump
|
1172
|
+
@source = ''
|
1173
|
+
@tight = @usage =~ /\[\s*tight\s*\]/i
|
1174
|
+
@caller = caller()
|
1175
|
+
|
1176
|
+
# VESTIGAL DEBUGGING CODE
|
1177
|
+
if @@debug
|
1178
|
+
f = File.new(".CODE.rb","w") and
|
1179
|
+
f.puts( code() ) and
|
1180
|
+
f.close()
|
1181
|
+
end
|
1182
|
+
|
1183
|
+
# DO THE PARSE (IF APPROPRIATE)
|
1184
|
+
if opts.size == 2
|
1185
|
+
return nil unless parse(source)
|
1186
|
+
else
|
1187
|
+
return nil unless parse()
|
1188
|
+
end
|
1189
|
+
|
1190
|
+
end # initialize
|
1191
|
+
|
1192
|
+
|
1193
|
+
# Parse the parameter description and in some cases,
|
1194
|
+
# optionally eval it, too.
|
1195
|
+
def parse(*opts)
|
1196
|
+
source = opts[0]
|
1197
|
+
_args = nil
|
1198
|
+
_get_nextline = proc { nil }
|
1199
|
+
|
1200
|
+
if source
|
1201
|
+
case source
|
1202
|
+
when Method
|
1203
|
+
_get_nextline = source
|
1204
|
+
_args = _get_nextline.call(self)
|
1205
|
+
source = '[METHOD]'
|
1206
|
+
when Proc
|
1207
|
+
_get_nextline = source
|
1208
|
+
_args = _get_nextline.call(self)
|
1209
|
+
source = '[PROC]'
|
1210
|
+
when IO
|
1211
|
+
if source.fileno > 0 && source.tty?
|
1212
|
+
_get_nextline = method(:_get_nextline)
|
1213
|
+
_args = $stdin.readline
|
1214
|
+
source = '<STDIN>'
|
1215
|
+
else
|
1216
|
+
_args = source.readlines.join(' ')
|
1217
|
+
_args.tr!('\t\n',' ')
|
1218
|
+
end
|
1219
|
+
when :build, :skip
|
1220
|
+
return 0
|
1221
|
+
when Array
|
1222
|
+
if source.length() == 1 && !source[0] ||
|
1223
|
+
source[0] == "-BUILD" ||
|
1224
|
+
source[0] == "-SKIP"
|
1225
|
+
return 0
|
1226
|
+
elsif source.length() == 2 && source[0] == "-ARGV"
|
1227
|
+
if !source[1] or !source[1] === Array
|
1228
|
+
raise 'Error: parse(["-ARGV"]) not passed an array as second parameter.'
|
1229
|
+
end
|
1230
|
+
_args = source[1].map { |i| i.tr( " \t\n", "\0\0\0" ) }.join(' ')
|
1231
|
+
source = '<ARRAY>'
|
1232
|
+
elsif source.length() == 1 && source[0] == "-STDIN"
|
1233
|
+
_get_nextline = method(:_get_nextline)
|
1234
|
+
_args = $stdin.readline
|
1235
|
+
source = '<STDIN>'
|
1236
|
+
elsif source.length() == 1 && source[0] == '-CONFIG'
|
1237
|
+
progname = "#{$0}rc"
|
1238
|
+
progname.sub!(%r#.*/#,'')
|
1239
|
+
home = ENV['HOME'] || ''
|
1240
|
+
_args, source = _load_sources( _get_nextline,
|
1241
|
+
[ home+"/.#{progname}",
|
1242
|
+
".#{progname}" ] )
|
1243
|
+
else
|
1244
|
+
# Bunch of files to load passed to parse()
|
1245
|
+
_args, source = _load_sources( _get_nextline, source )
|
1246
|
+
end
|
1247
|
+
when String # else/case LITERAL STRING TO PARSE
|
1248
|
+
_args = source.dup
|
1249
|
+
source = source[0,7] + '...' if source && source.length() > 7
|
1250
|
+
source = "\"#{source[0..9]}\""
|
1251
|
+
else
|
1252
|
+
raise "Unknown source type for Getopt::Declare::parse"
|
1253
|
+
end # case
|
1254
|
+
return 0 unless _args
|
1255
|
+
source = " (in #{source})"
|
1256
|
+
else
|
1257
|
+
_args = ARGV.map { |i| i.tr( " \t\n", "\0\0\0" ) }.join(' ')
|
1258
|
+
source = ''
|
1259
|
+
end
|
1260
|
+
|
1261
|
+
@source = source
|
1262
|
+
begin
|
1263
|
+
err = eval( code(@caller) )
|
1264
|
+
if $@
|
1265
|
+
# oops, something wrong... exit
|
1266
|
+
puts "#{$!}: #{$@.inspect}"
|
1267
|
+
exit(1)
|
1268
|
+
end
|
1269
|
+
if !err
|
1270
|
+
exit(1)
|
1271
|
+
end
|
1272
|
+
rescue
|
1273
|
+
raise
|
1274
|
+
end
|
1275
|
+
|
1276
|
+
|
1277
|
+
true
|
1278
|
+
end
|
1279
|
+
|
1280
|
+
def type(*t)
|
1281
|
+
Getopt::Declare::ScalarArg::addtype(t)
|
1282
|
+
end
|
1283
|
+
|
1284
|
+
# Print out version information and maybe exit
|
1285
|
+
def version(*t)
|
1286
|
+
prog = "#{$0}"
|
1287
|
+
begin
|
1288
|
+
filedate = File.stat( prog ).mtime.localtime()
|
1289
|
+
rescue
|
1290
|
+
filedate = 'Unknown date'
|
1291
|
+
end
|
1292
|
+
prog.sub!(%r#.*/#,'')
|
1293
|
+
r = ''
|
1294
|
+
if defined?($VERSION)
|
1295
|
+
r << "\n#{prog}: version #{$VERSION} (#{filedate})\n\n"
|
1296
|
+
else
|
1297
|
+
r << "\n#{prog}: version dated #{filedate}\n\n"
|
1298
|
+
end
|
1299
|
+
|
1300
|
+
if t.empty?
|
1301
|
+
return r
|
1302
|
+
else
|
1303
|
+
puts r
|
1304
|
+
exit t[0]
|
1305
|
+
end
|
1306
|
+
end
|
1307
|
+
|
1308
|
+
# Print out usage information
|
1309
|
+
def usage(*opt)
|
1310
|
+
|
1311
|
+
t = @usage
|
1312
|
+
|
1313
|
+
lastflag = nil
|
1314
|
+
lastdesc = nil
|
1315
|
+
usage = ''
|
1316
|
+
|
1317
|
+
while t.length() > 0
|
1318
|
+
|
1319
|
+
# COMMENT:
|
1320
|
+
t.sub!(/\A[ \t]*#.*\n/,".") and next
|
1321
|
+
|
1322
|
+
# TYPE DIRECTIVE:
|
1323
|
+
se = DelimScanner::new( t )
|
1324
|
+
|
1325
|
+
if t =~ /\A\s*\[pvtype:/
|
1326
|
+
if action = se.extractBracketed("[")
|
1327
|
+
t.sub!(Regexp::quote( action ).to_re,'')
|
1328
|
+
t.sub!(/\A[ \t]*\n/,"")
|
1329
|
+
next
|
1330
|
+
end
|
1331
|
+
end
|
1332
|
+
|
1333
|
+
# ACTION
|
1334
|
+
codeblockDelimiters = {
|
1335
|
+
'{' => '}'
|
1336
|
+
}
|
1337
|
+
se = DelimScanner::new( t )
|
1338
|
+
if action = se.extractCodeblock(codeblockDelimiters)
|
1339
|
+
t.sub!(Regexp::quote( action ).to_re,'')
|
1340
|
+
t.sub!(/\A[ \t]*\n/,"")
|
1341
|
+
decfirst = 0 unless !decfirst.nil?
|
1342
|
+
next
|
1343
|
+
end
|
1344
|
+
|
1345
|
+
|
1346
|
+
# ARG + DESC:
|
1347
|
+
if t.sub!(re_argument,"")
|
1348
|
+
decfirst = 0 unless !decfirst.nil?
|
1349
|
+
spec = "#$1".expand_tabs!()
|
1350
|
+
desc = "#$2".expand_tabs!()
|
1351
|
+
|
1352
|
+
while t.gsub!(re_more_desc, '')
|
1353
|
+
desc += "#$1".expand_tabs!
|
1354
|
+
end
|
1355
|
+
|
1356
|
+
next if desc =~ /\[\s*undocumented\s*\]/i
|
1357
|
+
|
1358
|
+
uoff = 0
|
1359
|
+
spec.gsub!(/(<[a-zA-Z]\w*):([^>]+)>/e) { |i|
|
1360
|
+
uoff += 1 + "#$2".length() and "#$1>"
|
1361
|
+
}
|
1362
|
+
spec.gsub!(/\t/,"=")
|
1363
|
+
|
1364
|
+
ditto = desc =~ /\A\s*\[ditto\]/
|
1365
|
+
desc.gsub!(/^\s*\[.*?\]\s*\n/m,"")
|
1366
|
+
desc.gsub!(BracketDirectives,'')
|
1367
|
+
#desc.gsub!(/\[.*?\]/,"")
|
1368
|
+
|
1369
|
+
|
1370
|
+
if ditto
|
1371
|
+
desc = (lastdesc ? _ditto(lastflag,lastdesc,desc) : "" )
|
1372
|
+
elsif desc =~ /\A\s*\Z/
|
1373
|
+
next
|
1374
|
+
else
|
1375
|
+
lastdesc = desc
|
1376
|
+
end
|
1377
|
+
|
1378
|
+
spec =~ /\A\s*(\S+)/ and lastflag = "#$1"
|
1379
|
+
|
1380
|
+
desc.sub!(/\s+\Z/, "\n")
|
1381
|
+
usage += spec + ' ' * uoff + desc
|
1382
|
+
next
|
1383
|
+
end
|
1384
|
+
|
1385
|
+
|
1386
|
+
|
1387
|
+
# OTHERWISE, DECORATION
|
1388
|
+
if t.sub!(/((?:(?!\[pvtype:).)*)(\n|(?=\[pvtype:))/,"")
|
1389
|
+
desc = "#$1"+("#$2"||'')
|
1390
|
+
#desc.gsub!(/^(\s*\[.*?\])+\s*\n/m,'')
|
1391
|
+
#desc.gsub!(/\[.*?\]/,'') # eliminates anything in brackets
|
1392
|
+
if @tight || desc !~ /\A\s*\Z/
|
1393
|
+
desc.gsub!(BracketDirectives,'')
|
1394
|
+
next if desc =~ /\A\s*\Z/
|
1395
|
+
end
|
1396
|
+
decfirst = 1 unless !decfirst.nil? or desc =~ /\A\s*\Z/
|
1397
|
+
usage += desc
|
1398
|
+
end
|
1399
|
+
|
1400
|
+
end #while
|
1401
|
+
|
1402
|
+
required = ''
|
1403
|
+
|
1404
|
+
for arg in @args
|
1405
|
+
required += ' ' + arg.desc + ' ' if arg.required
|
1406
|
+
end
|
1407
|
+
|
1408
|
+
usage.gsub!(/\255/,"[/") # REINSTATE ESCAPED '['s
|
1409
|
+
|
1410
|
+
required.gsub!(/<([a-zA-Z]\w*):[^>]+>/,'<\1>')
|
1411
|
+
required.rstrip!
|
1412
|
+
|
1413
|
+
helpcmd = Getopt::Declare::Arg::besthelp
|
1414
|
+
versioncmd = Getopt::Declare::Arg::bestversion
|
1415
|
+
|
1416
|
+
|
1417
|
+
header = ''
|
1418
|
+
unless @source.nil?
|
1419
|
+
header << version()
|
1420
|
+
prog = "#{$0}"
|
1421
|
+
prog.sub!(%r#.*/#,'')
|
1422
|
+
header << "Usage: #{prog} [options]#{required}\n"
|
1423
|
+
header << " #{prog} #{helpcmd}\n" if helpcmd
|
1424
|
+
header << " #{prog} #{versioncmd}\n" if versioncmd
|
1425
|
+
header << "\n" unless decfirst && decfirst == 1 && usage =~ /\A[ \t]*\n/
|
1426
|
+
end
|
1427
|
+
|
1428
|
+
header << "Options:\n" unless decfirst && decfirst == 1
|
1429
|
+
|
1430
|
+
usage.sub!(/[\s\n]+$/, '')
|
1431
|
+
|
1432
|
+
if opt.empty?
|
1433
|
+
return header + usage + "\n"
|
1434
|
+
end
|
1435
|
+
|
1436
|
+
pager = $stdout
|
1437
|
+
#begin
|
1438
|
+
# eval('require "IO/Pager";')
|
1439
|
+
# pager = IO::Pager.new()
|
1440
|
+
#rescue
|
1441
|
+
#end
|
1442
|
+
|
1443
|
+
#usage.sub!(/\A[\s\n]+/m, '')
|
1444
|
+
pager.puts "#{header}#{usage}"
|
1445
|
+
exit(opt[0]) if opt[0]
|
1446
|
+
end
|
1447
|
+
|
1448
|
+
attr_accessor :unused
|
1449
|
+
|
1450
|
+
|
1451
|
+
# Return list of used parameters (after parsing)
|
1452
|
+
def used
|
1453
|
+
used = @cache.keys
|
1454
|
+
return used.join(' ')
|
1455
|
+
end
|
1456
|
+
|
1457
|
+
@@m = []
|
1458
|
+
|
1459
|
+
# Main method to generate code to be evaluated for parsing.
|
1460
|
+
def code(*t)
|
1461
|
+
package = t[0] || ''
|
1462
|
+
code = %q%
|
1463
|
+
|
1464
|
+
|
1465
|
+
@_deferred = []
|
1466
|
+
@_errormsg = nil
|
1467
|
+
@_finished = nil
|
1468
|
+
|
1469
|
+
begin
|
1470
|
+
|
1471
|
+
begin
|
1472
|
+
undef :defer
|
1473
|
+
undef :reject
|
1474
|
+
undef :finish
|
1475
|
+
rescue
|
1476
|
+
end
|
1477
|
+
|
1478
|
+
def defer(&i)
|
1479
|
+
@_deferred.push( i )
|
1480
|
+
end
|
1481
|
+
|
1482
|
+
def reject(*i)
|
1483
|
+
if !i || i[0]
|
1484
|
+
@_errormsg = i[1] if i[1]
|
1485
|
+
throw :paramout
|
1486
|
+
end
|
1487
|
+
end
|
1488
|
+
|
1489
|
+
def finish(*i)
|
1490
|
+
if i.size
|
1491
|
+
@_finished = i
|
1492
|
+
else
|
1493
|
+
@_finished = true
|
1494
|
+
end
|
1495
|
+
end
|
1496
|
+
|
1497
|
+
@unused = []
|
1498
|
+
@cache = {}
|
1499
|
+
_FOUND_ = {}
|
1500
|
+
_errors = 0
|
1501
|
+
_invalid = {}
|
1502
|
+
_lastprefix = nil
|
1503
|
+
|
1504
|
+
_pos = 0 # current position to match from
|
1505
|
+
_nextpos = 0 # next position to match from
|
1506
|
+
|
1507
|
+
catch(:alldone) do
|
1508
|
+
while !@_finished
|
1509
|
+
begin
|
1510
|
+
catch(:arg) do
|
1511
|
+
@_errormsg = nil
|
1512
|
+
|
1513
|
+
# This is used for clustering of flags
|
1514
|
+
while _lastprefix
|
1515
|
+
substr = _args[_nextpos..-1]
|
1516
|
+
substr =~ /^(?!\s|\0|\Z)% +
|
1517
|
+
Getopt::Declare::Arg::negflagpat() + %q%/ or
|
1518
|
+
begin
|
1519
|
+
_lastprefix=nil
|
1520
|
+
break
|
1521
|
+
end
|
1522
|
+
"#{_lastprefix}#{substr}" =~ /^(% +
|
1523
|
+
Getopt::Declare::Arg::posflagpat() + %q%)/ or
|
1524
|
+
begin
|
1525
|
+
_lastprefix=nil
|
1526
|
+
break
|
1527
|
+
end
|
1528
|
+
_args = _args[0.._nextpos-1] + _lastprefix + _args[_nextpos..-1]
|
1529
|
+
break
|
1530
|
+
end # while _lastprefix
|
1531
|
+
|
1532
|
+
% + '' + %q%
|
1533
|
+
_pos = _nextpos if _args
|
1534
|
+
|
1535
|
+
usage(0) if _args && gindex(_args,/\G(% + @helppat + %q%)(\s|\0|\Z)/,_pos)
|
1536
|
+
version(0) if _args && _args =~ /\G(% + @verspat + %q%)(\s|\0|\Z)/
|
1537
|
+
%
|
1538
|
+
|
1539
|
+
for arg in @args
|
1540
|
+
code << arg.code(self,package)
|
1541
|
+
end
|
1542
|
+
|
1543
|
+
code << %q%
|
1544
|
+
|
1545
|
+
if _lastprefix
|
1546
|
+
_pos = _nextpos + _lastprefix.length()
|
1547
|
+
_lastprefix = nil
|
1548
|
+
next
|
1549
|
+
end
|
1550
|
+
|
1551
|
+
_pos = _nextpos
|
1552
|
+
|
1553
|
+
_args && _pos = gindex( _args, /\G[\s|\0]*(\S+)/, _pos ) or throw(:alldone)
|
1554
|
+
|
1555
|
+
if @_errormsg
|
1556
|
+
$stderr.puts( "Error#{source}: #{@_errormsg}\n" )
|
1557
|
+
else
|
1558
|
+
@unused.push( @@m[0] )
|
1559
|
+
end
|
1560
|
+
|
1561
|
+
_errors += 1 if @_errormsg
|
1562
|
+
|
1563
|
+
end # catch(:arg)
|
1564
|
+
|
1565
|
+
ensure # begin
|
1566
|
+
_pos = 0 if _pos.nil?
|
1567
|
+
_nextpos = _pos if _args
|
1568
|
+
if _args and _args.index( /\G(\s|\0)*\Z/, _pos )
|
1569
|
+
_args = _get_nextline.call(self) if !@_finished
|
1570
|
+
throw(:alldone) unless _args
|
1571
|
+
_pos = _nextpos = 0
|
1572
|
+
_lastprefix = ''
|
1573
|
+
end # if
|
1574
|
+
end # begin/ensure
|
1575
|
+
end # while @_finished
|
1576
|
+
end # catch(:alldone)
|
1577
|
+
end # begin
|
1578
|
+
|
1579
|
+
%
|
1580
|
+
|
1581
|
+
|
1582
|
+
################################
|
1583
|
+
# Check for required arguments #
|
1584
|
+
################################
|
1585
|
+
for arg in @args
|
1586
|
+
next unless arg.required
|
1587
|
+
|
1588
|
+
code << %q%unless _FOUND_['% + arg.name + %q%'] %
|
1589
|
+
|
1590
|
+
if @mutex[arg.name]
|
1591
|
+
for m in @mutex[arg.name]
|
1592
|
+
code << %q# or _FOUND_['# + m + %q#']#
|
1593
|
+
end
|
1594
|
+
end
|
1595
|
+
|
1596
|
+
code << %q%
|
1597
|
+
$stderr.puts "Error#{@source}: required parameter '% + arg.name + %q%' not found."
|
1598
|
+
_errors += 1
|
1599
|
+
end
|
1600
|
+
%
|
1601
|
+
|
1602
|
+
end
|
1603
|
+
|
1604
|
+
########################################
|
1605
|
+
# Check for arguments requiring others #
|
1606
|
+
########################################
|
1607
|
+
|
1608
|
+
for arg in @args
|
1609
|
+
next unless arg.requires
|
1610
|
+
|
1611
|
+
code << %q%
|
1612
|
+
if _FOUND_['% + arg.name + %q%'] && !(% + arg.requires +
|
1613
|
+
%q%)
|
1614
|
+
$stderr.puts "Error#{@source}: parameter '% + arg.name + %q%' can only be specified with '% + arg.requires + %q%'"
|
1615
|
+
_errors += 1
|
1616
|
+
end
|
1617
|
+
%
|
1618
|
+
end
|
1619
|
+
|
1620
|
+
code << %q%
|
1621
|
+
#################### Add unused arguments
|
1622
|
+
if _args && _nextpos > 0 && _args.length() > 0
|
1623
|
+
@unused.replace( @unused + _args[_nextpos..-1].split(' ') )
|
1624
|
+
end
|
1625
|
+
|
1626
|
+
for i in @unused
|
1627
|
+
i.tr!( "\0", " " )
|
1628
|
+
end
|
1629
|
+
|
1630
|
+
%
|
1631
|
+
|
1632
|
+
if @strict
|
1633
|
+
code << %q%
|
1634
|
+
#################### Handle strict flag
|
1635
|
+
unless _nextpos < ( _args ? _args.length : 0 )
|
1636
|
+
for i in @unused
|
1637
|
+
$stderr.puts "Error#{@source}: unrecognizable argument ('#{i}')"
|
1638
|
+
_errors += 1
|
1639
|
+
end
|
1640
|
+
end
|
1641
|
+
%
|
1642
|
+
end
|
1643
|
+
|
1644
|
+
code << %q%
|
1645
|
+
#################### Print help hint
|
1646
|
+
if _errors > 0 && !@source.nil?
|
1647
|
+
$stderr.puts "\n(try '#$0 % + Getopt::Declare::Arg::besthelp + %q%' for more information)"
|
1648
|
+
end
|
1649
|
+
|
1650
|
+
## cannot just assign unused to ARGV in ruby
|
1651
|
+
unless @source != ''
|
1652
|
+
ARGV.clear
|
1653
|
+
@unused.map { |i| ARGV.push(i) }
|
1654
|
+
end
|
1655
|
+
|
1656
|
+
unless _errors > 0
|
1657
|
+
for i in @_deferred
|
1658
|
+
begin
|
1659
|
+
i.call
|
1660
|
+
rescue => e
|
1661
|
+
STDERR.puts "Action in Getopt::Declare specification produced:\n#{e}"
|
1662
|
+
_errors += 1
|
1663
|
+
end
|
1664
|
+
end
|
1665
|
+
end
|
1666
|
+
|
1667
|
+
!(_errors>0) # return true or false (false for errors)
|
1668
|
+
|
1669
|
+
%
|
1670
|
+
return code
|
1671
|
+
end
|
1672
|
+
|
1673
|
+
|
1674
|
+
# Inspect cache (not the declare object)
|
1675
|
+
def inspect
|
1676
|
+
return nil if !@cache
|
1677
|
+
t = ''
|
1678
|
+
|
1679
|
+
@cache.each { |a,b|
|
1680
|
+
t << a + " => "
|
1681
|
+
case b
|
1682
|
+
when Hash
|
1683
|
+
t << "{"
|
1684
|
+
i = []
|
1685
|
+
b.each { |c,d|
|
1686
|
+
i.push( " '#{c}' => " + d.inspect )
|
1687
|
+
}
|
1688
|
+
t << i.join(',')
|
1689
|
+
t << " }"
|
1690
|
+
else
|
1691
|
+
t << b.inspect
|
1692
|
+
end
|
1693
|
+
t << "\n"
|
1694
|
+
}
|
1695
|
+
t << "Unused: " + unused.join(', ')
|
1696
|
+
end
|
1697
|
+
|
1698
|
+
# Iterator for Getopt::Declare (travels thru all cache keys)
|
1699
|
+
def each(&t)
|
1700
|
+
@cache.each(&t)
|
1701
|
+
end
|
1702
|
+
|
1703
|
+
# Operator to easily create new value in of Getopt::Declare
|
1704
|
+
def []=(name,val)
|
1705
|
+
@cache = {} unless @cache
|
1706
|
+
@cache[name] = val
|
1707
|
+
end
|
1708
|
+
|
1709
|
+
# Operator to easily return cache of Getopt::Declare
|
1710
|
+
def [](name)
|
1711
|
+
if @cache
|
1712
|
+
return @cache[name]
|
1713
|
+
else
|
1714
|
+
return nil
|
1715
|
+
end
|
1716
|
+
end
|
1717
|
+
|
1718
|
+
# Operator to return number of flags set
|
1719
|
+
def size
|
1720
|
+
return 0 unless @cache
|
1721
|
+
return @cache.keys.size
|
1722
|
+
end
|
1723
|
+
|
1724
|
+
attr :mutex
|
1725
|
+
attr :helppat
|
1726
|
+
attr :verspat
|
1727
|
+
attr :strict
|
1728
|
+
attr :clump
|
1729
|
+
attr :source
|
1730
|
+
|
1731
|
+
end # class Declare
|
1732
|
+
|
1733
|
+
end # module Getopt
|
1734
|
+
|
1735
|
+
|
1736
|
+
|
1737
|
+
|
1738
|
+
__END__
|
1739
|
+
|