Really Cheap Command-Line Option Parsing in Ruby

前端 未结 20 1154
野的像风
野的像风 2020-11-30 16:57

EDIT: Please, please, please read the two requirements listed at the bottom of this post before replying. People keep posting their new gems and li

20条回答
  •  广开言路
    2020-11-30 17:07

    Suppose a command has at most one action and arbitrary number of options like this:

    cmd.rb
    cmd.rb action
    cmd.rb action -a -b ...
    cmd.rb action -ab ...
    

    The parsing without validation may be like this:

    ACTION = ARGV.shift
    OPTIONS = ARGV.join.tr('-', '')
    
    if ACTION == '***'
      ...
      if OPTIONS.include? '*'
        ...
      end
      ...
    end
    

提交回复
热议问题