Class: Raph::Parser::GroupedArgParser

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/raph/parser/grouped_arg_parser.rb

Overview

Grouped arguments are arguments that follows a group - a specific argument. In other words flags and assignments represents groups and arguments followed them are grouped arguments.

Next example: '--group1', '1', '2', '--group2', 'three' has two groups (:group1 and :group2) with corresponding grouped arguments (['1', '2'], ['three'])

Instance Method Summary collapse

Methods inherited from BaseParser

#id

Constructor Details

#initializeGroupedArgParser


17
18
19
20
# File 'lib/raph/parser/grouped_arg_parser.rb', line 17

def initialize
  @flag_parser = FlagParser.new
  @assignment_parser = AssignmentParser.new
end

Instance Method Details

#group?(arg) ⇒ Boolean


37
38
39
# File 'lib/raph/parser/grouped_arg_parser.rb', line 37

def group?(arg)
  @flag_parser.flag?(arg) || @assignment_parser.assignment?(arg)
end

#parse(args) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/raph/parser/grouped_arg_parser.rb', line 22

def parse(args)
  groups = {}
  current_group = nil

  args.each do |arg|
    if group? arg
      current_group = to_underscored_sym arg
      groups[current_group] = []
    else
      groups[current_group].push(arg) if current_group
    end
  end
  groups
end