/lib/option_parser.rb

http://github.com/tgunr/passengerpane · Ruby · 28 lines · 22 code · 3 blank · 3 comment · 9 complexity · 1c7ed999ad294478484d8bebd157c757 MD5 · raw file

  1. class OptionParser
  2. def self.parse(argv)
  3. return [{},[]] if argv.empty?
  4. options = {}
  5. rest = []
  6. switch = nil
  7. for value in argv
  8. # values is a switch
  9. if value[0] == 45
  10. switch = value.slice((value[1] == 45 ? 2 : 1)..-1)
  11. options[switch] = nil
  12. else
  13. if switch
  14. # we encountered a switch so this
  15. # value belongs to that switch
  16. options[switch] = value
  17. switch = nil
  18. else
  19. rest << value
  20. end
  21. end
  22. end
  23. [options, rest]
  24. end
  25. end