class Getopt::Declare::ScalarArg
Class used to handle scalar (ie.non-array) parameters
Attributes
name[R]
nows[R]
type[R]
Public Class Methods
_reset_stdtype()
click to toggle source
(re)set standard types
# File lib/Getopt/Declare.rb, line 127 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)
click to toggle source
Add a new (user defined) type to the standard types
# File lib/Getopt/Declare.rb, line 226 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
new(name, type, nows)
click to toggle source
Constructor
# File lib/Getopt/Declare.rb, line 248 def initialize(name, type, nows) @name = name @type = type @nows = nows end
stdactions(name)
click to toggle source
Given the name of a type, return its corresponding action(s)
# File lib/Getopt/Declare.rb, line 207 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)
click to toggle source
Given a standard type name, return the corresponding regex pattern or nil
# File lib/Getopt/Declare.rb, line 191 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
Public Instance Methods
cachecode(ownerflag, itemcount)
click to toggle source
Return string with code to cache argument in Getopt::Declare
‘s cache
# File lib/Getopt/Declare.rb, line 326 def cachecode(ownerflag, itemcount) if itemcount > 1 " @cache['#{ownerflag}']['<#{@name}>'] = #{@name}\n" else " @cache['#{ownerflag}'] = #{@name}\n" end end
code(*t)
click to toggle source
Return string with code to process parameter
# File lib/Getopt/Declare.rb, line 280 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
conversion()
click to toggle source
Based on parameter type, default conversion to apply
# File lib/Getopt/Declare.rb, line 312 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)
click to toggle source
Create regexp to match parameter
# File lib/Getopt/Declare.rb, line 255 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)
click to toggle source
Helps build regex that matches parameters of flags Wraps parameter passed for #$1, etc. matching
# File lib/Getopt/Declare.rb, line 341 def ows(g) return '[\s|\0]*(' + g + ')' unless @nows return '(' + g + ')' end
stdtype(name)
click to toggle source
# File lib/Getopt/Declare.rb, line 201 def stdtype(name) ScalarArg.stdtype(name) end
trailer()
click to toggle source
Helps build regex that matches parameters of flags
# File lib/Getopt/Declare.rb, line 335 def trailer nil # MEANS TRAILING PARAMETER VARIABLE (in Perl,was '') end