Class: Perennial::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/perennial/argument_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ ArgumentParser

Returns a new instance of ArgumentParser.



8
9
10
11
12
# File 'lib/perennial/argument_parser.rb', line 8

def initialize(array)
  @raw_arguments = array.dup
  @arguments     = []
  @results       = {}
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



6
7
8
# File 'lib/perennial/argument_parser.rb', line 6

def arguments
  @arguments
end

Class Method Details

.parse(args = ARGV) ⇒ Object



30
31
32
33
34
# File 'lib/perennial/argument_parser.rb', line 30

def self.parse(args = ARGV)
  parser = self.new(args)
  parser.parse!
  return parser.arguments, parser.to_hash
end

Instance Method Details

#parse!Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/perennial/argument_parser.rb', line 18

def parse!
  while !@raw_arguments.empty?
    current = @raw_arguments.shift
    if option?(current)
      process_option(current, @raw_arguments.first) 
    else
      @arguments.push current
    end
  end
  return nil
end

#to_hashObject



14
15
16
# File 'lib/perennial/argument_parser.rb', line 14

def to_hash
  @results ||= {}
end