Using ruby's OptionParser to parse sub-commands

后端 未结 4 1204
情话喂你
情话喂你 2021-01-31 04:39

I\'d like to be able to use ruby\'s OptionParser to parse sub-commands of the form

COMMAND [GLOBAL FLAGS] [SUB-COMMAND [SUB-COMMAND FLAGS]]

lik

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 04:44

    It looks like the OptionParser syntax has changed some. I had to use the following so that the arguments array had all of the options not parsed by the opts object.

    begin
      opts.order!(arguments)
    rescue OptionParser::InvalidOption => io
      # Prepend the invalid option onto the arguments array
      arguments = io.recover(arguments)
    rescue => e
      raise "Argument parsing failed: #{e.to_s()}"
    end
    

提交回复
热议问题