# dry-cli adding value transformation based upon \`type:\`

**URL:** <https://discourse.dry-rb.org/t/dry-cli-adding-value-transformation-based-upon-type/1748>\
**Category:** Ideas\
**Created:** [November 4, 2023, 7:18pm UTC](https://discourse.dry-rb.org/t/dry-cli-adding-value-transformation-based-upon-type/1748 "2023-11-04T19:18:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MadBomber](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/madbomber/32/929_2.png) [@MadBomber](https://discourse.dry-rb.org/u/MadBomber)\
**Post date:** [November 4, 2023, 7:18pm UTC](https://discourse.dry-rb.org/t/dry-cli-adding-value-transformation-based-upon-type/1748/1 "2023-11-04T19:18:15Z")

</div>

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.

```ruby
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.
