Class: OptionParser
- Inherits:
-
Object
- Object
- OptionParser
- Defined in:
- lib/option_parser.rb
Overview
Parser for command line options
Class Method Summary collapse
-
.parse(argv) ⇒ Object
Parses ARGV from a Ruby script and returns options as a hash and arguments as a list.
Class Method Details
.parse(argv) ⇒ Object
Parses ARGV from a Ruby script and returns options as a hash and arguments as a list.
OptionParser.parse(%w(create --username manfred)) #=>
[{"username"=>"manfred"}, ["create"]]
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/option_parser.rb', line 8 def self.parse(argv) return [{},[]] if argv.empty? = {} rest = [] switch = nil for value in argv # value is a switch if value[0] == 45 switch = value.slice((value[1] == 45 ? 2 : 1)..-1) [switch] = nil else if switch # we encountered another switch so this # value belongs to the last switch [switch] = value switch = nil else rest << value end end end [, rest] end |