Class: Getopt::Declare::ArrayArg

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

Overview

Class used to handle array arguments

Instance Attribute Summary

Attributes inherited from ScalarArg

#name, #nows, #type

Instance Method Summary collapse

Methods inherited from ScalarArg

_reset_stdtype, addtype, #conversion, #initialize, #ows, stdactions, stdtype, #stdtype, #trailer

Constructor Details

This class inherits a constructor from Getopt::Declare::ScalarArg

Instance Method Details

#cachecode(ownerflag, itemcount) ⇒ Object

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



395
396
397
398
399
400
401
402
# File 'lib/Getopt/Declare.rb', line 395

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

#code(*t) ⇒ Object

Return string with code to process array parameter



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/Getopt/Declare.rb', line 362

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

	code = <<-EOS
		  _VAR_ = %q|<#{@name}>|
		  _VAL_ = nil
		  #{@name} = (@@m[#{pos1}]||'').split(' ').map { |i| 
                                                      i.tr("\\0", " ") }
	EOS

  # Handle conversion to proper type
  c = conversion
  if c
	 code << "		  #{@name}.map! { |i| i#{c} }\n"
  end

  actions = Getopt::Declare::ScalarArg::stdactions(@type)
  if actions.size > 0
	 code << "		  for _VAL_ in #{@name}\n"
	 for i in actions
code << "		       #{i}\n"
	 end
	 code << "		  end\n\n"
  end
  return code
end

#matcher(g) ⇒ Object

Create regexp to match array



354
355
356
357
358
359
# File 'lib/Getopt/Declare.rb', line 354

def matcher(g)
  suffix = !g.nil? ? '([\s\0]+)' : ''
  scalar = super  # contains regex to match a scalar element
  # we match one obligatory element, and one or more optionals ')*'
  return scalar + '(?:[\s\0]+' + scalar + ')*' + suffix
end