Class: Synoption::OptionSet

Inherits:
OptionList show all
Includes:
Logue::Loggable
Defined in:
lib/synoption/set.rb

Constant Summary collapse

@@options_for_class =

maps from the option set 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

Methods inherited from OptionList

#<<, #add, #find_by_name, #has_option?, #inspect, #to_command_line

Constructor Details

#initialize(options = Array.new) ⇒ OptionSet

Returns a new instance of OptionSet.



32
33
34
35
36
37
38
39
40
# File 'lib/synoption/set.rb', line 32

def initialize options = Array.new
  super 

  cls = self.class
  while cls != OptionSet
    add_options_for_class cls
    cls = cls.superclass
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/synoption/set.rb', line 30

def options
  @options
end

#unprocessedObject (readonly)

Returns the value of attribute unprocessed.



17
18
19
# File 'lib/synoption/set.rb', line 17

def unprocessed
  @unprocessed
end

Class Method Details

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/synoption/set.rb', line 19

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

  define_method name do
    instance_eval do
      opt = instance_variable_get '@' + name.to_s          
      opt.value
    end
  end
end

Instance Method Details

#add_options_for_class(cls) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/synoption/set.rb', line 42

def add_options_for_class cls
  opts = @@options_for_class[cls]

  opts.each do |option|
    name = option[:name]
    cls = option[:class]
    args = option[:args]
    opt = cls.new(*args)
    
    add opt
    instance_variable_set '@' + name.to_s, opt
  end
end

#check_for_valid_optionsObject



94
95
96
97
98
99
100
# File 'lib/synoption/set.rb', line 94

def check_for_valid_options 
  @unprocessed.each do |opt|
    if opt.start_with? '-'
      raise OptionException.new "error: option: #{opt} invalid for #{name}"
    end
  end
end

#post_process_all(options_processed) ⇒ Object



102
103
104
105
106
# File 'lib/synoption/set.rb', line 102

def post_process_all options_processed
  options_processed.each do |opt|
    opt.post_process self, @unprocessed
  end
end

#process(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/synoption/set.rb', line 61

def process args
  options_processed = Array.new

  @unprocessed = args

  aborted = false
  
  while !@unprocessed.empty?
    if @unprocessed[0] == '--'
      @unprocessed.delete_at 0
      aborted = true
      break
    end

    processed = false

    options.each do |opt|
      if opt.process @unprocessed
        processed = true
        options_processed << opt
      end
    end

    break unless processed
  end

  unless aborted
    check_for_valid_options 
  end

  post_process_all options_processed
end

#unset(key) ⇒ Object



56
57
58
59
# File 'lib/synoption/set.rb', line 56

def unset key
  opt = find_by_name key
  opt && opt.unset
end