It looks like the type:
parameter is only being used in the option to setup flags when its value is :boolean AND bring in arrays when its value is :array.
Here is some code that adds a TRANSFORMERS Hash object to the Parser module. Then inside the opts.on
look that new object is queried to see if the option’s Hash has a transformation Proc for the given type. If so, the Proc is called on the incoming value.
module Dry
class CLI
# Parse command line arguments and options
#
# @since 0.1.0
# @api private
module Parser
TRANSFORMERS = {
integer: -> (v) { v&.to_i },
float: -> (v) { v&.to_f}
}
# @since 0.1.0
# @api private
#
def self.call(command, arguments, prog_name)
original_arguments = arguments.dup
parsed_options = {}
OptionParser.new do |opts|
command.options.each do |option|
opts.on(*option.parser_options) do |value|
if TRANSFORMERS.has_key?(option.options[:type])
value = TRANSFORMERS[option.options[:type]].call(value)
end
parsed_options[option.name.to_sym] = value
end
end
opts.on_tail("-h", "--help") do
return Result.help
end
end.parse!(arguments)
parsed_options = command.default_params.merge(parsed_options)
parse_required_params(command, arguments, prog_name, parsed_options)
rescue ::OptionParser::ParseError
Result.failure("ERROR: \"#{prog_name}\" was called with arguments \"#{original_arguments.join(" ")}\"") # rubocop:disable Metrics/LineLength
end
# ... and the rest of the module is as is
Now within the application program we can either make use of the default TRANSFORMERS or the app can over-ride Dry::CLI::Parser::TRANSFORMERS with its on “types” and Procs.