Class: Getopt::Declare::ScalarArg

Inherits:
Object
  • Object
show all
Defined in:
lib/Getopt/Declare.rb

Overview

Class used to handle scalar (ie.non-array) parameters

Direct Known Subclasses

ArrayArg

Constant Summary collapse

@@stdtype =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, nows) ⇒ ScalarArg

Constructor



249
250
251
252
253
# File 'lib/Getopt/Declare.rb', line 249

def initialize(name, type, nows)
	@name = name
	@type = type
	@nows = nows
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



243
244
245
# File 'lib/Getopt/Declare.rb', line 243

def name
  @name
end

#nowsObject (readonly)

Returns the value of attribute nows.



245
246
247
# File 'lib/Getopt/Declare.rb', line 245

def nows
  @nows
end

#typeObject (readonly)

Returns the value of attribute type.



244
245
246
# File 'lib/Getopt/Declare.rb', line 244

def type
  @type
end

Class Method Details

._reset_stdtypeObject

(re)set standard types



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
# File 'lib/Getopt/Declare.rb', line 128

def ScalarArg._reset_stdtype
	@@stdtype = {
	  ':i'	=> { :pattern => '(?:(?:%T[+-]?)%D+)' },
	  ':n'	=> { :pattern => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
	    '(?:%T[eE](?:[+-])?%D+)?|%T\.%D+(?:%T[eE](?:[+-])?%D+)?))',
	  },
    ':s'	=> { :pattern => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
    ':qs'	=> { :pattern => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
	  ':id'	=> { :pattern => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
	  ':d'	=> { 
      :pattern => '(?:%T(?:\S|\0))+',
	    :action => %q%
        reject( (_VAL_.nil? || !test(?d, _VAL_) ),
         "in parameter '#{_PARAM_}' (\"#{_VAL_}\" is not a directory)")%
	  },
	  ':if'	=> { 
      :pattern => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
	    :action => %q%
        reject( (_VAL_.nil? || _VAL_ != "-" && !test(?r, _VAL_) ),
         "in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not readable)")%
	  },
	  ':of'	=> { 
      :pattern => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
	    :action => %q%
      reject( (_VAL_.nil? || _VAL_ != "-" && 
               test(?r, _VAL_) && !test(?w, _VAL_)), 
        "in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not writable)")%
	  },
	  ''	=> { :pattern => ':s', :ind => 1 },

	  ':+i'	=> { 
      :pattern => ':i',
	    :action => %q%reject( _VAL_ <= 0, 
                             "in parameter '#{_PARAM_}' (#{_VAL_} must be an integer greater than zero)")%,
	    :ind => 1
	  },

	  ':+n'	=> { 
      :pattern => ':n',
	    :action => %q%reject( _VAL_ <= 0.0, 
                             "in parameter '#{_PARAM_}' (#{_VAL_} must be a number greater than zero)")%,
	    :ind => 1
	  },

	  ':0+i' => { 
      :pattern => ':i',
	    :action => %q%reject( _VAL_ < 0, 
                             "in parameter '#{_PARAM_}' (#{_VAL_} must be an positive integer)")%,
	    :ind  => 1
	  },
	  
	  ':0+n' => { 
	    :pattern => ':n',
	    :action => %q%reject( _VAL_ < 0, 
                             "in parameter '#{_PARAM_}' (#{_VAL_} must be an positive number)")%,
	    :ind => 1
	  },
	}

end

.addtype(abbrev, pattern, action, ref) ⇒ Object

Add a new (user defined) type to the standard types



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/Getopt/Declare.rb', line 227

def ScalarArg.addtype(abbrev, pattern, action, ref)

	typeid = ":#{abbrev}"
	unless (pattern =~ /\S/)
	  pattern = ":s"
	  ref = 1
	end
	
	@@stdtype[typeid] = {}
	@@stdtype[typeid][:pattern] = "(?:#{pattern})" if pattern && !ref
	@@stdtype[typeid][:pattern] = ":#{pattern}" if pattern && ref
	@@stdtype[typeid][:action]  = action if action
	@@stdtype[typeid][:ind]     = ref

end

.stdactions(name) ⇒ Object

Given the name of a type, return its corresponding action(s)



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/Getopt/Declare.rb', line 208

def ScalarArg.stdactions(name)
	seen = {}
	actions = []
	while (!seen[name] && @@stdtype[name] && @@stdtype[name][:ind])
	  seen[name] = 1
	  if @@stdtype[name][:action]
	    actions.push( @@stdtype[name][:action] )
	  end
	  name = @@stdtype[name][:pattern]
	end

	if @@stdtype[name] && @@stdtype[name][:action]
	  actions.push( @@stdtype[name][:action] )
	end

	return actions
end

.stdtype(name) ⇒ Object

Given a standard type name, return the corresponding regex pattern or nil



192
193
194
195
196
197
198
199
200
# File 'lib/Getopt/Declare.rb', line 192

def ScalarArg.stdtype(name)
	seen = {}
	while (!seen[name] && @@stdtype[name] && @@stdtype[name][:ind])
	  seen[name] = 1; name = @@stdtype[name][:pattern]
	end

	return nil if seen[name] || !@@stdtype[name]
	@@stdtype[name][:pattern]
end

Instance Method Details

#cachecode(ownerflag, itemcount) ⇒ Object

Return string with code to cache argument in Getopt::Declare’s cache



327
328
329
330
331
332
333
# File 'lib/Getopt/Declare.rb', line 327

def cachecode(ownerflag, itemcount)
  if itemcount > 1
    "		  @cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n"
  else
    "		  @cache['#{ownerflag}'] = #{@name}\n"
  end
end

#code(*t) ⇒ Object

Return string with code to process parameter



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
# File 'lib/Getopt/Declare.rb', line 281

def code(*t)
	if t[0]
	  pos1 = t[0].to_s
	else
	  pos1 = '0'
	end

	c = conversion
	c = "\n	          _VAL_ = _VAL_#{c} if _VAL_" if c

	code = <<-EOS
     _VAR_ = %q|<#{@name}>|
     _VAL_ = @@m[#{pos1}]
     _VAL_.tr!("\\0"," ") if _VAL_#{c}
EOS

  actions = Getopt::Declare::ScalarArg::stdactions(@type)

	  for i in actions
	    next if i.nil?
	    # i.sub!(/(\s*\{)/, '\1 module '+t[1])
	    code << "
		  begin
                  #{i}
		  end
"
	  end

	code << "		  #{@name} = _VAL_\n"
end

#conversionObject

Based on parameter type, default conversion to apply



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/Getopt/Declare.rb', line 313

def conversion
  pat = @@stdtype[@type] ? @@stdtype[@type][:pattern] : ''
  [ @type, pat ].each { |t|
    case t
    when /^\:0?(\+)?i$/
      return '.to_i'
    when /^\:0?(\+)?n$/
      return '.to_f'
    end
  }
  return nil
end

#matcher(g) ⇒ Object

Create regexp to match parameter



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/Getopt/Declare.rb', line 256

def matcher(g)
	trailing = g ? '(?!'+Regexp::quote(g)+')' : ''

	# Find type in list of standard (and user) types
	stdtype = stdtype(@type)

	# Handle stdtypes that are specified as regex in parameter
	if (!stdtype && @type =~ %r"\A:/([^/]+)/\Z" )
	  stdtype = "#$1"
	end

	if stdtype.nil?
	  raise "Error: bad type in Getopt::Declare parameter variable specification near '<#{@name}#{@type}>'\nValid types are:\n" + @@stdtype.keys.inspect
	end

	stdtype = stdtype.dup  # make a copy, as we'll change it in place
	stdtype.gsub!(/\%D/,"(?:#{trailing}\\d)")
	stdtype.gsub!(/\%T/,trailing)
	unless ( stdtype.sub!("\%F","") )
	  stdtype = Getopt::Declare::Arg::negflagpat + stdtype
	end
	return "(?:#{stdtype})"
end

#ows(g) ⇒ Object

Helps build regex that matches parameters of flags Wraps parameter passed for #$1, etc. matching



342
343
344
345
# File 'lib/Getopt/Declare.rb', line 342

def ows(g)
  return '[\s|\0]*(' + g + ')' unless @nows
  return '(' + g + ')'
end

#stdtype(name) ⇒ Object



202
203
204
# File 'lib/Getopt/Declare.rb', line 202

def stdtype(name)
	ScalarArg.stdtype(name)
end

#trailerObject

Helps build regex that matches parameters of flags



336
337
338
# File 'lib/Getopt/Declare.rb', line 336

def trailer 
  nil	# MEANS TRAILING PARAMETER VARIABLE (in Perl,was '')
end