Restrict `OptionParser` argument to enum values

Hi, I have a quesiton, please help, thanks.

When i use OptionParser, i want to limit the users input with a Enum, how to do that?

e.g. check following code,

parser.on(
      "-e ENGINE",
      "--engine=ENGINE",
      "Specify engine used for translate, support bing|youdao for now.
") do |e|
      case e
      when "bing"
        engine = "bing"
      when "youdao"
        engine = "youdao"
      else
        STDERR.puts "Supported options: -e bing|youado"
        exit
      end
    end

The user passed args with -e ARGS is a string, i have to raise a error to limit the user only can support -e youdao or -e bing, this really a case which enum be good at, right?

This is basically a question of user input working with enum, so, is it possible?

Something like

unless engine = Engine.parse? e
  STDERR.puts "Supported options: #{Engine.names.map(&.downcase).join ", "}"
  exit 1
end

# ...
2 Likes