Class: Ragol::OptionSet

Inherits:
Object
  • Object
show all
Includes:
Logue::Loggable
Defined in:
lib/ragol/optset.rb

Direct Known Subclasses

OptProc::OptionSet, Synoption::OptionSet

Constant Summary collapse

@@options_for_class =

maps from an OptionSet class to the valid options for that class.

Hash.new { |h, k| h[k] = Array.new }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ OptionSet

Returns a new instance of OptionSet.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ragol/optset.rb', line 26

def initialize(*options)
  if options[0] && options[0].kind_of?(Hash)
    data = options[0][:data]
    options = data.collect do |optdata|
      optargs = OptionArguments.new(optdata)

      opttype = optargs[:valuetype]
      clstype = OptionArguments::VAR_TYPES[opttype]

      if clstype
        clsstr = clstype.to_s
        require 'ragol/' + clsstr + '_option'
        clssym = (clsstr.capitalize + 'Option').to_sym
        optcls = ::Ragol.const_get(clssym)
        optcls.new(optargs)
      else
        Ragol::Option.new(optargs)
      end
    end
  end
  
  @options = options

  cls = self.class
  while cls <= OptionSet
    opts = self.class.options_for_class(cls)
    
    opts.each do |option|
      args = option[:args]
      opt = option[:class].new(*args)
      
      add opt
    end
    
    cls = cls.superclass
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/ragol/optset.rb', line 24

def options
  @options
end

Class Method Details

.has_option(optcls, optargs = Hash.new) ⇒ Object



16
17
18
# File 'lib/ragol/optset.rb', line 16

def self.has_option optcls, optargs = Hash.new
  @@options_for_class[self] << { :class => optcls, :args => optargs }
end

.options_for_class(cls) ⇒ Object



20
21
22
# File 'lib/ragol/optset.rb', line 20

def self.options_for_class cls
  @@options_for_class[cls]
end

Instance Method Details

#<<(option) ⇒ Object



76
77
78
# File 'lib/ragol/optset.rb', line 76

def << option
  add option
end

#add(option) ⇒ Object



80
81
82
83
# File 'lib/ragol/optset.rb', line 80

def add option
  @options << option
  option
end

#find_by_name(name) ⇒ Object



72
73
74
# File 'lib/ragol/optset.rb', line 72

def find_by_name name
  @options.find { |opt| opt.name == name }
end

#find_matching_option(results) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/ragol/optset.rb', line 120

def find_matching_option results
  type, opt = get_best_match(results)

  unless type
    raise OptionException.new "#{name}: invalid option '#{results.current_arg}'"
  end

  [ type, opt ]
end

#get_best_match(results) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ragol/optset.rb', line 85

def get_best_match results
  tag_matches = Hash.new { |h, k| h[k] = Array.new }
  negative_match = nil
  regexp_match = nil

  options.each do |opt|
    if mt = opt.matchers.match_type?(results.current_arg)
      case mt[0]
      when :tag_match
        tag_matches[mt[1]] << opt
      when :negative_match
        negative_match = opt
      when :regexp_match
        regexp_match = opt
      end
    end
  end

  if tag_matches.keys.any?
    highest = tag_matches.keys.sort[-1]
    opts = tag_matches[highest]
    if opts.size > 1
      optstr = opts.collect { |opt| '(' + opt.to_s + ')' }.join(', ')
      raise "ambiguous match of '#{results.current_arg}'; matches options: #{optstr}"
    end
    [ :tag_match, opts.first ]
  elsif negative_match
    [ :negative_match, negative_match ]
  elsif regexp_match
    [ :regexp_match, regexp_match ]
  else
    nil
  end
end

#inspectObject



68
69
70
# File 'lib/ragol/optset.rb', line 68

def inspect
  @options.collect { |opt| opt.inspect }.join("\n")
end

#nameObject



64
65
66
# File 'lib/ragol/optset.rb', line 64

def name
  @name ||= self.class.to_s.sub(%r{.*?(\w+)OptionSet}, '\1').downcase
end

#process(args, results = Ragol::Results.new(options, args)) ⇒ Object



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
# File 'lib/ragol/optset.rb', line 148

def process args, results = Ragol::Results.new(options, args)
  options_processed = Array.new
  
  while !results.args_empty?
    if results.end_of_options?
      results.shift_arg
      break
    elsif results.current_arg[0] != '-'
      break
    end

    option = set_option(results)
    if option
      options_processed << option
    else
      break
    end
  end

  options_processed.each do |opt|
    opt.post_process self, results, results.args
  end
  
  results
end

#read_rclines(lines, results = Ragol::Results.new(options, nil)) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ragol/optset.rb', line 180

def read_rclines lines, results = Ragol::Results.new(options, nil)
  lines.each do |line|
    line.sub!(%r{\#.*}, '')
    next if line.empty?
    name, val = line.split(%r{\s*:\s*})
    if opt = @options.detect { |op| op.match_rc? name }
      opt.set_option_value val, name, results
    end
  end

  results
end

#set_option(results) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ragol/optset.rb', line 130

def set_option results
  type, opt = find_matching_option(results)

  case type
  when :tag_match
    arg = results.next_arg
    opt.set_value_for_tag results, arg
  when :negative_match
    arg = results.next_arg
    opt.set_value_negative results, arg
  when :regexp_match
    arg = results.next_arg
    opt.set_value_regexp results, arg
  end

  opt
end

#unset(results, key) ⇒ Object



174
175
176
177
178
# File 'lib/ragol/optset.rb', line 174

def unset results, key
  if opt = find_by_name(key)
    results.unset_value opt.name
  end
end