Implement argument and option type casting#146
Implement argument and option type casting#146gustavothecoder wants to merge 1 commit intodry-rb:mainfrom
Conversation
| elsif float? | ||
| value.to_f | ||
| elsif argument? && (boolean? || flag?) | ||
| %w[0 f false off].include?(value.downcase) ? false : true |
| return value if value.nil? | ||
|
|
||
| if integer? | ||
| value.to_i |
There was a problem hiding this comment.
The issue with this is that casting "abc" to 0 might be considered too frivolous by many. Maybe something like Dry::Types::Params could work better here, but I'm not sure we want to pull a dependency to dry-cli.
If not, using Integer(value) would be a better choice here IMO, but we'd need to handle the exceptions in a better way than just propagating the ArgumentError.
|
I'm more and more convinced that this should be some kind of Dry Types plugin. @timriley I noticed that Dry CLI does not have Dry Core in dependencies. Is this a design decision that Dry CLI should not have any dependencies, or is this just that it was not needed yet? Would be nice to use |
As reported in the issue #129, although we have the
typeconfig 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:
The output: