Class: PVN::OptionSet

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/synoption/set.rb

Direct Known Subclasses

Command::OptionSet

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

Constructor Details

#initialize(options = Array.new) ⇒ OptionSet

Returns a new instance of OptionSet.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/synoption/set.rb', line 37

def initialize options = Array.new
  @options = options
  @arguments = Array.new

  opts = @@options_for_class[self.class]

  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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



35
36
37
# File 'lib/synoption/set.rb', line 35

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/synoption/set.rb', line 34

def options
  @options
end

#unprocessedObject (readonly)

Returns the value of attribute unprocessed.



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

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
29
30
31
# File 'lib/synoption/set.rb', line 19

def has_option name, optcls, optargs = Hash.new
  attr_reader name

  @@options_for_class[self] << { :name => name, :class => optcls, :args => optargs }

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

Instance Method Details

#<<(option) ⇒ Object



127
128
129
# File 'lib/synoption/set.rb', line 127

def << option
  @options << option
end

#add(option) ⇒ Object



131
132
133
134
# File 'lib/synoption/set.rb', line 131

def add option
  @options << option
  option
end

#as_command_lineObject



136
137
138
# File 'lib/synoption/set.rb', line 136

def as_command_line
  to_command_line + arguments
end

#check_for_valid_optionsObject



113
114
115
116
117
118
119
# File 'lib/synoption/set.rb', line 113

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

#find_by_name(name) ⇒ Object



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

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

#has_option?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/synoption/set.rb', line 61

def has_option? name
  find_by_name name
end

#inspectObject



53
54
55
# File 'lib/synoption/set.rb', line 53

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

#post_process_all(options_processed) ⇒ Object



121
122
123
124
125
# File 'lib/synoption/set.rb', line 121

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

#process(args) ⇒ Object



80
81
82
83
84
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
# File 'lib/synoption/set.rb', line 80

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

#to_command_lineObject



65
66
67
68
69
70
71
72
73
# File 'lib/synoption/set.rb', line 65

def to_command_line
  cmdline = Array.new
  @options.each do |opt|
    if cl = opt.to_command_line
      cmdline.concat cl
    end
  end
  cmdline
end

#unset(key) ⇒ Object



75
76
77
78
# File 'lib/synoption/set.rb', line 75

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