rails / thor

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

`enum` should be enforced for all values of `type: array` #783

Closed movermeyer closed 1 year ago

movermeyer commented 2 years ago

🌈

enum was added in https://github.com/rails/thor/pull/237, but it originally only validated type: string values.

Validation of type: numeric values was added in https://github.com/rails/thor/pull/386

I expected all values of my type: array option to also be validated.

Example

cli.rb:

require "thor"

class MyCLI < Thor
  desc "array_with_enum [--fruits=apple banana]", "Demo of using an enum with an array"
  option :fruits, type: :array, enum: ["apple", "banana", "clementine"]
  def array_with_enum
    puts "fruits: #{options[:fruits]}"
  end
end

MyCLI.start(ARGV)

This works:

ruby ./cli.rb array_with_enum --fruits=banana apple
fruits: ["banana", "apple"]

This also works:

ruby ./cli.rb array_with_enum --fruits=kiwi
fruits: ["kiwi"]

but shouldn't since "kiwi" is not a fruit in the enum.