class Fuelcell::Parser::BaseHandler
Parsing the command line involves a command object, raw args array and the final options hash. Handlers use the command object to determine specifications on options and args, they will add and remove raw args and add to the options hash. This class provides helper methods used to encapsulate the command tasks needed by every handler
Public Instance Methods
Anything that does not start with an “-” is considered an arg nil is not considered an arg or an opt, since all elements in ARGV are strings
@param text [String] represents an element from ARGV @return [Boolean]
# File lib/fuelcell/parser/base_handler.rb, line 16 def arg?(text) return false if text.nil? !text.to_s.start_with?('-') end
Assigns opt name and value into the opts hash. It provides a common place for validation checks and casting.
@param opts [Hash] holds processed opts @param opt [Fuelcell::OptDefinition] @param value [String] value found in raw_args @param raw_arg [String] used for error msg, the actual opt string @return the casted value
# File lib/fuelcell/parser/base_handler.rb, line 56 def assign_opt_value(opts, opt, value, raw_arg) fail "#{raw_arg} is a flag and can not accept values" if opt.flag? opts[opt.name] = cast(opt.type, value) end
Delegate the cast based on type
@param type [Symbol] @param value [String] @param raw_args [Array] @return casted value
# File lib/fuelcell/parser/base_handler.rb, line 112 def cast(type, value) case type when :numeric then cast_numeric(value) when :bool then cast_bool(value) when :hash then cast_hash(value) when :array then cast_array(value) else value.to_s end end
Collect all values in raw args upto the first option and put then into an array
@params value [String] the first value of the array @params raw_args [Array] raw args used to collect remaining items @return [Array]
# File lib/fuelcell/parser/base_handler.rb, line 85 def cast_array(value) value.to_s.split(',') end
# File lib/fuelcell/parser/base_handler.rb, line 69 def cast_bool(value) case value.to_s.downcase when 'true', 'yes', '1' then true when 'false', 'no', '0' then false else fail ArgumentError, "expecting bool value like " + "(true,false,yes,no,1,0), #{value} is invalid" end end
A hash key value pair is assumed to be in the form key:value. Collect all args that are not opts and process them.
@param value [String] the first key value pair @param raw_args [Array] raw args to collect remaining hash values @return [Hash]
# File lib/fuelcell/parser/base_handler.rb, line 95 def cast_hash(value) list = cast_array(value) result = {} list.each do |item| key, value = item.split(':', 2) result[key] = value end result end
# File lib/fuelcell/parser/base_handler.rb, line 62 def cast_numeric(value) unless value =~ /[-+]?\d*\.\d+|\d+/ fail ArgumentError, "expecting a numeric type, '#{value}' is invalid" end value.index('.') ? value.to_f : value.to_i end
Search for an opt definition in the given command object
@param cmd [Fuelcell::Command] command found on the cli @param name [String] name of the object @return [Fuelcell::OptDefinition]
# File lib/fuelcell/parser/base_handler.rb, line 26 def find_opt(cmd, name) opt = cmd.find_opt(name) fail "option #{name} is not registered" unless opt opt end
A flag has a value of true when its found
@param opts [Hash] processed opts @param opt [Fuelcell::OptDefinition] @return [Boolean]
# File lib/fuelcell/parser/base_handler.rb, line 128 def found_opt_flag(opts, opt) opts[opt.name] = true end
Allows handlers to match the first raw arg against a regex or someother condition and if it fails that arg will be put back into the raw args otherwise the handler will process it
params arg [String] returns [Boolean, String]
# File lib/fuelcell/parser/base_handler.rb, line 39 def take_first_arg(args) arg = args.shift unless yield arg args.unshift arg return false end arg end