Class: Ragol::OptionArguments

Inherits:
Hash
  • Object
show all
Defined in:
lib/ragol/args.rb

Constant Summary collapse

VAR_TYPES =
{
  :boolean => :boolean,
  :string => :string,
  :float => :float,
  :integer => :fixnum,
  :fixnum => :fixnum,
  :regexp => :regexp,
}
OLD_OPTIONS =
{
  :regexps => [ Regexp.new('--fo+'), Regexp.new('--ba*r') ],
  :tags => [ '--foo', '-b' ],
  :arg => [ [ :boolean, :string, :float, :integer, :fixnum, :regexp ], [ :optional, :required, :none ] ],
  :set => Proc.new { },
  :rcnames => [ "foo" ],
  :rc => [ ]
}
NEW_OPTIONS =
{
  :regexps => [ Regexp.new('--fo+'), Regexp.new('--ba*r') ],
  :tags => [ '--foo', '-b' ],
  :rcnames => [ 'foo', 'foobar' ],
  :takesvalue => [ true, :optional, false ],
  :valuetype => [ :boolean, :string, :float, :integer, :fixnum, :regexp ],
  :valueregexp => Regexp.new('(one|two|three)'),
  :default => nil,
  :process => Proc.new { |val| },
  :postproc => Proc.new { |optset, results, unprocessed| },
  :description => "a description"
}

Instance Method Summary collapse

Constructor Details

#initialize(origargs = Hash.new) ⇒ OptionArguments

Returns a new instance of OptionArguments.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ragol/args.rb', line 39

def initialize origargs = Hash.new
  super()

  if origargs[:arg]
    if re = origargs[:arg].find { |x| x.kind_of?(Regexp) }
      self[:valueregexp] = re
      self[:valuetype] = :regexp
    else
      self[:valuetype] = Ragol::HashUtil.hash_array_value VAR_TYPES, origargs[:arg]
    end
    
    self[:takesvalue] = if self[:valuetype] == :boolean
                          false
                        else
                          hasvaluetype = self[:valuetype] != nil
                          takes = { :optional => :optional, :required => true, :none => hasvaluetype, nil => hasvaluetype }
                          Ragol::HashUtil.hash_array_value takes, origargs[:arg]
                        end
  else
    Ragol::HashUtil.copy_hash self, origargs, [ [ :takesvalue, :valuereq ], [ :valuetype ], [ :valueregexp ] ]
    if self[:valuetype]
      self[:takesvalue] ||= true
    end
  end

  fields = [
            [ :regexps, :regexp, :res, :re ],
            [ :tags ],
            [ :process, :set ],
            [ :postproc ],
            [ :rcnames, :rcname, :rc ],
            [ :default ],
            [ :unsets, :unset ],
            [ :description ],
           ]
  Ragol::HashUtil.copy_hash self, origargs, fields
end