Class: WLST::ArgParser
- Inherits:
-
Object
- Object
- WLST::ArgParser
- Defined in:
- lib/wlst/wlst.rb
Instance Attribute Summary collapse
-
#converter ⇒ Object
readonly
Returns the value of attribute converter.
Instance Method Summary collapse
-
#initialize(definitions = {}) ⇒ ArgParser
constructor
A new instance of ArgParser.
- #parse(*args) ⇒ Object
Constructor Details
#initialize(definitions = {}) ⇒ ArgParser
Returns a new instance of ArgParser.
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 |
# File 'lib/wlst/wlst.rb', line 92 def initialize definitions = {} @definitions = definitions @converter = { :array => lambda do |arg| return arg if arg.is_a? Array arg.split(',') end, :boolean => lambda do |arg| if arg.is_a? FalseClass or arg.is_a? TrueClass return arg.to_s.capitalize.to_sym end return :False if arg.downcase == 'false' return :True if arg.downcase == 'true' :True end, :int => lambda do |arg| arg.to_i end, :float => lambda do |arg| arg.to_f end, :string => lambda do |arg| arg end } end |
Instance Attribute Details
#converter ⇒ Object (readonly)
Returns the value of attribute converter.
90 91 92 |
# File 'lib/wlst/wlst.rb', line 90 def converter @converter end |
Instance Method Details
#parse(*args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wlst/wlst.rb', line 119 def parse *args params = {} args.each do |arg| value = arg.split '=' key = value.delete_at(0).gsub('-', '_') definition = @definitions[key] converter = @converter[definition['type'].to_sym] value = value.join('=') params[key] = converter.call value if converter params[key] ||= value end @definitions.each do |name, defs| unless params.has_key? name raise "#{name} not specified" if defs['required'] converter = @converter[defs['type'].to_sym] params[name] = converter.call defs['default'] if converter params[name] ||= defs['default'] end end params end |