rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.11k stars 552 forks source link

Enums should support Range #774

Closed phene closed 1 year ago

phene commented 2 years ago

There are cases, such as numerics, where you may want to enforce that an option's value is within an acceptable range of values.

method_option :min_success_rate, type: :numeric, enum: 1..100, description: 'The minimum success rate'

This may be preferable to adding :min or :max options for such validations. The problem is that use of join when displaying the enumeration when the validation fails, as Range#join does not exist.

Options to do this include:

  1. Implement Range#join. I personally don't like this option, as we could end up allocating large arrays for no real gain, which would clutter the output.
  2. Improve the way the enumeration is printed when you specify a value outside the enumeration. Instead of relying on join, perhaps detect that it is a Range and print it out in a friendlier way that clearly indicates the min and max values of that range.

I'm happy to submit a PR if I can get some guidance on which option would be preferred or if there is another proposed solution.

dorner commented 2 years ago

I definitely wouldn't add an extra method to the base class. I think a PR with the second option could be a good approach!

phene commented 2 years ago

PR: https://github.com/rails/thor/pull/775