Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. module ParseOpts
  2. # returns a hash of command line options from a specified array.
  3. # expected format is "--key val --switch --key val..."
  4. # returns something like {key: val, switch: true, key: val}
  5.  
  6. def self.parse options
  7. options_hash = {}
  8. options.each_index do |i|
  9. if options[i][0] == '-'
  10. key = options[i]
  11.  
  12. # scrub out leading hyphens as needed
  13. while key[0] == '-'
  14. key = key[1...key.length]
  15. end
  16.  
  17. val = options[i+1]
  18.  
  19. if val == nil || val[0] == '-'
  20. val = true
  21. end
  22.  
  23. options_hash[key.to_sym] = val
  24. end
  25. end
  26. return options_hash
  27. end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement