Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement argument and option type casting #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gustavothecoder
Copy link

As reported in the issue #129, although we have the type config when defining arguments and options, we don't type cast the value for the user, which for some may seem like a bug.

This PR implement a simple type casting logic for some types. Since some types that users currently receive as string will start to be received as another type (e.g. integer), this is a breaking change.

The CLI bellow demonstrates all available types:

require 'dry/cli'

module Demo
  extend Dry::CLI::Registry

  class TypeCast < Dry::CLI::Command
    argument :a_int, type: :integer
    argument :a_flo, type: :float
    argument :a_boo, type: :boolean
    argument :a_fla, type: :flag
    argument :a_str, type: :string
    argument :a_arr, type: :array
    option :o_int, type: :integer
    option :o_flo, type: :float
    option :o_boo, type: :boolean
    option :o_fla, type: :flag
    option :o_str, type: :string
    option :o_arr, type: :array

    def call(a_int:, a_flo:, a_boo:, a_fla:, a_str:, a_arr:, o_int:, o_flo:, o_boo:, o_fla:, o_str:, o_arr:, **)
      puts <<~HEREDOC
        Arguments:
          #{a_int}\t\t\t#{a_int.class}
          #{a_flo}\t\t\t#{a_flo.class}
          #{a_boo}\t\t\t#{a_boo.class}
          #{a_fla}\t\t\t#{a_fla.class}
          #{a_str}\t#{a_str.class}
          #{a_arr}\t#{a_arr.class}
        Options:
          #{o_int}\t\t\t#{o_int.class}
          #{o_flo}\t\t\t#{o_flo.class}
          #{o_boo}\t\t\t#{o_boo.class}
          #{o_fla}\t\t\t#{o_fla.class}
          #{o_str}\t#{o_str.class}
          #{o_arr}\t#{o_arr.class}
      HEREDOC
    end
  end

  register "type-cast", TypeCast
end

Dry::CLI.new(Demo).call

The output:

$ demo type-cast 42 42.42 t off 'This is a string' 1 2 3 --o_int=42 --o_flo=42.42 --no-o_boo --o_fla --o_str='this is a string' --o_arr=1,2,3

# Before:
Arguments:
  42                    String
  42.42                 String
  t                     String
  off                   String
  This is a string      String
  ["1", "2", "3"]       Array
Options:
  42                    String
  42.42                 String
  false                 FalseClass
  true                  TrueClass
  this is a string      String
  ["1", "2", "3"]       Array

# After:
Arguments:
  42                    Integer
  42.42                 Float
  true                  TrueClass
  false                 FalseClass
  This is a string      String
  ["1", "2", "3"]       Array
Options:
  42                    Integer
  42.42                 Float
  false                 FalseClass
  true                  TrueClass
  this is a string      String
  ["1", "2", "3"]       Array

elsif float?
value.to_f
elsif argument? && (boolean? || flag?)
%w[0 f false off].include?(value.downcase) ? false : true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used rails as a base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant