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,

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

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: #{ ", "}"
  exit 1

# ...
1 Like