Class: PVN::OptionSet
Direct Known Subclasses
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
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#unprocessed ⇒ Object
readonly
Returns the value of attribute unprocessed.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(option) ⇒ Object
- #add(option) ⇒ Object
- #as_command_line ⇒ Object
- #check_for_valid_options ⇒ Object
- #find_by_name(name) ⇒ Object
- #has_option?(name) ⇒ Boolean
-
#initialize(options = Array.new) ⇒ OptionSet
constructor
A new instance of OptionSet.
- #inspect ⇒ Object
- #post_process_all(options_processed) ⇒ Object
- #process(args) ⇒ Object
- #to_command_line ⇒ Object
- #unset(key) ⇒ Object
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 = Array.new @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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
35 36 37 |
# File 'lib/synoption/set.rb', line 35 def arguments @arguments end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/synoption/set.rb', line 34 def @options end |
#unprocessed ⇒ Object (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_line ⇒ Object
136 137 138 |
# File 'lib/synoption/set.rb', line 136 def as_command_line to_command_line + arguments end |
#check_for_valid_options ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/synoption/set.rb', line 113 def @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
61 62 63 |
# File 'lib/synoption/set.rb', line 61 def has_option? name find_by_name name end |
#inspect ⇒ Object
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 .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 = Array.new @unprocessed = args aborted = false while !@unprocessed.empty? if @unprocessed[0] == '--' @unprocessed.delete_at 0 aborted = true break end processed = false .each do |opt| if opt.process @unprocessed processed = true << opt end end break unless processed end unless aborted end post_process_all end |
#to_command_line ⇒ Object
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 |